好的,所以我在一个完全没有改变的网站上有代码。两天前我正在使用这个功能并且它有效,但是今天当我测试它时它就停止了工作。
出于某种原因,从.children()
选择器更改为.find()
修复它,这是我真正没想到的。
这是代码:
var ele = $("div").find("[data-productid='" + response.productId + "']").children(".wishlist-button");
if (response.addedToWishlist) {
$(ele).children(".button-2.active").removeClass("active");
$(ele).children(".remove-from-wishlist-button").addClass("active");
}
我知道.children()
只选择直接子级,所以这里的 html 说明了这一点:
<div class="wishlist-button">
<input type="button" value="Remove from wishlist" title="Remove from wishlist" class="button-2 remove-from-wishlist-button remove-from-wishlist-button-6475">
<input type="button" value="Add to wishlist" title="Add to wishlist" class="button-2 add-to-wishlist-button add-to-wishlist-button-6475 active">
</div>
当我将代码更改为此使用.find()
它时:
var ele = $("div").find("[data-productid='" + response.productId + "']").find(".wishlist-button");
if (response.addedToWishlist) {
$(ele).find(".button-2.active").removeClass("active");
$(ele).find(".remove-from-wishlist-button").addClass("active");
}
就像我说的那样,代码都没有改变,所以它停止工作很奇怪。我正在使用 jquery 版本 1.10.2 我想知道该.children()
功能是否已贬值,或者可能还有其他事情发生。
有谁知道这里发生了什么?我很担心,因为该.children()
功能在整个站点中被使用了数百次。谢谢