作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想遍历 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_
开头的元素。
$("#pageHeadings div[class^=menu_]").hide();
或者,您也可以使用 $(this).is('[class^=menu_]')
。
$("#pageHeadings div").each(function (index, val) {
if($(this).is('[class^=menu_]')) {
$(this).hide();
}
});
关于jquery - 遍历父 div 并使用匹配隐藏子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23419425/
我是一名优秀的程序员,十分优秀!