0) { $(this).css("bac-6ren">
gpt4 book ai didi

javascript "this"未能定位

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

我在第一个例子中做错了什么?它不是针对跨度并应用背景颜色。但第二个例子效果很好

第一。

    if($(".done").length > 0) {
$(this).css("background","red");
}

第二。

    if($(".done").length > 0) {
$(".done").css("background","red");
}

最佳答案

仅通过 jQuery 运行选择器不会影响 this 值。事实上,您根本无法在特定函数的范围内更改 this 值。

你可以试试这个:

if($(".done").length > 0) {
$('done').each(function() { $(this).css("background","red"); });
}

当然最好缓存 $(".done") 内容。在这种情况下,如果这确实是所有代码,则无论如何都没有理由对长度进行测试。

关于javascript "this"未能定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7236672/

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