【问题标题】:Loop through parent div and hide child classes with match循环遍历父 div 并使用 match 隐藏子类
【发布时间】:2014-06-18 14:57:55
【问题描述】:

我想循环遍历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 

所以我认为找到它们没问题。

【问题讨论】:

    标签: jquery css


    【解决方案1】:

    您可以使用[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();
        }
    });
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-23
    • 2016-12-27
    • 2012-07-22
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-20
    相关资源
    最近更新 更多