gpt4 book ai didi

jquery - 超过最大调用堆栈 jQuery $.each()

转载 作者:行者123 更新时间:2023-12-01 08:13:08 25 4
gpt4 key购买 nike

我正在尝试实现实时搜索过滤功能,并在 jQuery 中使用 $.each() 。现在,我只是尝试循环遍历每个元素并打印其文本,但遇到了最大调用堆栈超出的问题。该列表大约有 50-60 个项目,所以我认为大小根本不是问题,也许我遇到了一些无限递归,尽管我不这么认为。下面是我的相关 HTML:

<div id="brands">
<ul id="left_brands">
<li><a class="brand_check" href="javascript:void">Vineyard Vines</a></li>
<li><a class="brand_check" href="javascript:void">Brooks Brothers</a></li>
<li><a class="brand_check" href="javascript:void">Coast Apparel</a></li>
<!-- etc. -->
</ul>
</div>

这是给我错误的 jQuery 代码:

$("#brand_filter input").keyup(function() {
$.each("#brands li a", function() {
alert($(this).text());
});
});

非常感谢任何建议。

最佳答案

如果使用集合迭代器,则必须迭代 Jquery 对象,而不是选择器字符串。

所以只使用

$("#brand_filter input").keyup(function() {
$.each($("#brands li a"), function() {
alert($(this).text());
});
});

或者使用选择器语法

$("#brand_filter input").keyup(function() {
$("#brands li a").each(function() {
alert($(this).text());
});
});

关于jquery - 超过最大调用堆栈 jQuery $.each(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12256131/

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