gpt4 book ai didi

jquery - 遍历父 div 并使用匹配隐藏子类

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

我想遍历 pageHeadings 中的每个 div,如果子 div 的类匹配“menu_”,我想隐藏它。我到底错过了什么?

<div id="pageHeadings">
<div class="menu_practice">
<p>test</p>
<p>test2</p>
</div>
<div class="menu_about">
<p>test</p>
<p>test2</p>
</div>

$("#pageHeadings div").each(function (index, val) {
if($(this).attr('class').match("menu_")) {
$(this).hide();
}
});

获取:Uncaught TypeError: Cannot read property 'match' of undefined

console.log($(this).attr('class')); 给出:

col-md-1 col-md-offset-2 
undefined
col-md-1
undefined
menu_practice
col-md-1
undefined
menu_about
col-md-1
undefined
menu_contact

所以我认为它发现它们没问题。

最佳答案

您可以使用 [class^=menu_],它将查找类以 menu_ 开头的元素。

Example Here

$("#pageHeadings div[class^=menu_]").hide();

或者,您也可以使用 $(this).is('[class^=menu_]')

Example Here

$("#pageHeadings div").each(function (index, val) {
if($(this).is('[class^=menu_]')) {
$(this).hide();
}
});

关于jquery - 遍历父 div 并使用匹配隐藏子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23419425/

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