gpt4 book ai didi

Javascript 更新导致悲伤

转载 作者:行者123 更新时间:2023-11-30 18:18:44 24 4
gpt4 key购买 nike

无论如何我都不是 javascript 开发人员,但是我遇到了 JS 问题。

如果有人能帮我弄清楚问题出在哪里,那就太好了。

掉落的脚本是:

        $('button').each(function()
{
var curr_title = $(this).attr('title');

if(curr_title.length > 0) $(this).button({ icons: { primary: curr_title } });
else $(this).button();
});

错误是

TypeError: Cannot read property 'length' of undefined.

这在以前版本的代码中完全没有问题。然而,唯一的变化是更新了 Jquery。

有谁知道这是否可能是问题所在?

再次。如果我很愚蠢,我深表歉意。

最佳答案

此行返回的值是undefined(可能是因为它引用的按钮没有指定这样的属性。)

var curr_title = $(this).attr('title');

jQuery 改变了 attr() 的行为在 1.6 版本中

As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set.

所以if语句中的调用失败了。要“修复”这个问题,您可以像这样添加对 undefined 的检查:

if( (typeof curr_title != 'undefined') && (curr_title.length > 0)) {
$(this).button({ icons: { primary: curr_title } });
} else {
$(this).button();
}

关于Javascript 更新导致悲伤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12637114/

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