为什么这段代码没有返回false?

时间:2017-08-29 23:08:10

标签: javascript jquery

引用这个直截了当的对话: Is there an "exists" function for jQuery?

以下代码看到零,但仍然在第一种情况下评估为'true'。

以下是控制台结果:

  

找到listView:0/14

代码在对话窗口中......所以它可能会在底层页面上看到该元素。但是日志中的零则表示不然......

真的令人沮丧的边缘情况(或者我刚刚失去理智)。帮助赞赏。

if (('#listViewMenu').length) {   //  We are on taskpane list view
        console.log('Found listView: ' + $('#listViewMenu').length + ' / ' + ('#tableViewMenu').length);
        addList();      //hardcoded to list view only - auto load if there is an existing choice for Group
      }
      if (('#tableViewMenu').length) {    //  We are in dialog window table view
        addTable();
      }

1 个答案:

答案 0 :(得分:3)

在第一行,你忘了jQuery的$?

if (('#listViewMenu').length)

应该是

if ($('#listViewMenu').length)