gpt4 book ai didi

javascript - jquery 代码的 TypeOf 问题

转载 作者:行者123 更新时间:2023-11-28 18:31:36 25 4
gpt4 key购买 nike

我的某些代码有问题...

这是代码:

(function($) {
$.fn.inc = function(url, transform, post, t) {
return this.length && url ? this.each(function() {
t = $(this);
$.ajax({
url: url,
success: function(txt, jqXHR, textStatus) {
t.html($.isFunction(transform) ? transform(txt, url) : txt);
$.isFunction(post) && post(url, jqXHR, textStatus);
}
});
}) : this;
};

$(function() {
$('[class*="inc:"]').each(function() {
var match = /inc:(\S+)/.exec(this.className || '');
if(typeOf(match[1]) != "undefined")
match && $(this).inc(unescape(match[1]));
});

});

})(jQuery);

第 18 行指向此错误:

ReferenceError: typeOf is not defined


if(typeOf(match[1]) != "undefined")

代码有什么问题吗?

错误图片: enter image description here

此错误指向:

TypeError: a is null

**return (a.ownerDocument || a) !== n && m(a), t(a, b)**

最佳答案

您正在寻找的运算符typeof 全部小写。 JavaScript 区分大小写。

请注意,它是一个运算符,而不是一个函数,因此您不需要括号(尽管它们实际上不会造成伤害)。

另请注意 .exec() method可以返回 null,因此在尝试将其用作数组之前需要 match 是否为 null:

if (match != null && typeof match[1] != undefined)

或者只是:

if (match && typeof match[1] != undefined)

但是您不需要测试 match[1] 是否为 undefined,因为 if match 本身不 null 那么这意味着您的正则表达式匹配,包括子字符串匹配。所以以下应该没问题:

if (match)
$(this).inc(unescape(match[1]));

关于javascript - jquery 代码的 TypeOf 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37880509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com