检查元素是否存在

问题描述:


可能重复:

是否存在“存在” jQuery的函数

jQuery确定是否元素存在于页面

if(tr)当tr不是元素时,code>返回true,如何检查它是否是一个元素?

if(tr) is returning true when tr is not an element, how do I check whether it's an element that exists?

var tr = $('#parts-table .no-data').parent();
$('.delete', row).bind('click', function (e) {
  that.delete(e.currentTarget);
});
console.log(tr);
if (tr) //returns true when it shouldn't


检查其长度属性:

if(tr.length) {
    // exists
}

if(tr)总是求值为true,因为jQuery对象或任何 JavaScript对象,总是很简单。

if(tr) always evaluates to true because a jQuery object, or any JavaScript Object for that matter, is always truthy.