我的if语句没有被收听,无论如何都会触发脚本

时间:2017-10-20 04:12:03

标签: jquery html

我有一个简单的if语句(和概念),它不起作用......

我的代码:

if ( $("body").has('div.cntxt-rr') ) {
    $('.auto-rr').css('display', 'none');
}

因此,如果正文中有一个类为'.cntxt-rr'的元素,我想要一个带有'.auto-rr'类的div来显示:none。

目前结果是始终显示:无...

任何帮助都会很棒,

谢谢,

2 个答案:

答案 0 :(得分:1)

检查真值时,你应该使用.length。即

if ( $("body").has('div.cntxt-rr').length ) {
    $('.auto-rr').css('display', 'none');
}

$(“body”)。has('div.cntxt-rr')将始终求值为true,因为它总是返回一个对象

答案 1 :(得分:1)

检查

if($("body").find('div.cntxt-rr').length !== 0){
    $('.auto-rr').css('display', 'none');
}
相关问题