为什么这个条件不起作用

时间:2016-08-03 05:53:32

标签: javascript jquery

我使用了像这样的if条件

if (!$("tr[data-id='" + currenttrid + "'] > td:first-child").css("display", none)) {
  $("tr[data-id='" + parrentid + "'] > td:first-child button").addClass("collapse")
}

但我没有采取如何安排此代码

3 个答案:

答案 0 :(得分:1)

您需要获取显示属性,但是您将其置于if条件内。

使用.css("display")代替.css("display", none)

 if ($("tr[data-id='" + currenttrid + "'] > td:first-child").css("display")!="none" ) {
            $("tr[data-id='" + parrentid + "'] > td:first-child button").addClass("collapse")
        }

答案 1 :(得分:1)

我想你想要那个:

if (!$("tr[data-id='" + currenttrid + "'] > td:first-child").is(":visible")) {
    $("tr[data-id='" + parrentid + "'] > td:first-child button").addClass("collapse")
}

要检查元素是否显示,请使用 .is(“:visible”)

答案 2 :(得分:0)

基于布尔值执行if块。因此,conditnal语句将被评估为truefalse,并相应地执行。

您可以查看

if (!$("tr[data-id='" + currenttrid + "'] > td:first-child").css("display")==="none")) {  // will check the condition
  $("tr[data-id='" + parrentid + "'] > td:first-child button").addClass("collapse")
}

或者您可以使用is(selector)之类的

if($("#someElem").is(':visible')){
 // Rest of code
}