找到班级名称

时间:2015-04-28 04:40:38

标签: javascript

如何从

中找到课程
<div class="sidebar-toggle sidebar-collapse" id="sidebar-collapse">
    <i class="ace-icon fa fa-angle-double-right" data-icon2="ace-icon fa fa-angle-double-right" data-icon1="ace-icon fa fa-angle-double-left"></i>
</div>

我想找ace-icon fa fa-angle-double-right 我试过这个

    var sidebar_collapse = document.getElementById("sidebar-collapse");
    $(sidebar_collapse).on('click', function () {
        var angel_double_right = $(sidebar_collapse).find('.ace-icon fa fa-angle-double-right');
        if (angel_double_right) {
            alert("yeah has class");
            $(ticksign).css('display', 'none');
        }
    });

但不行。它每次都提醒

3 个答案:

答案 0 :(得分:1)

.find()返回一个jQuery对象,每次评估为true。所以你的if条件永远都是真的。如果您需要检查是否有任何元素与选择器实际匹配:

if (angel_double_right.length) {

答案 1 :(得分:0)

访问孩子,然后获取班级名称:

&#13;
&#13;
document.write(JSON.stringify(document.getElementById("sidebar-collapse").children[0].className));
&#13;
<div class="sidebar-toggle sidebar-collapse" id="sidebar-collapse">
    <i class="ace-icon fa fa-angle-double-right" data-icon2="ace-icon fa fa-angle-double-right" data-icon1="ace-icon fa fa-angle-double-left"></i>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

试试这个

$("sidebar-collapse").on('click', function () {
    var angel_double_right = $(this).find('.ace-icon.fa.fa-angle-double-right');
    if (angel_double_right.length>0) {
        alert("yeah has class");
        $(ticksign).css('display', 'none');
    }
});