如何在jQuery中检查元素的存在?
我当前的代码是这样的:
if ($(selector).length > 0) {
// Do something
}
Run Code Online (Sandbox Code Playgroud)
有更优雅的方式来解决这个问题吗?也许是插件或功能?
if(tr) 当tr不是元素时返回true,如何检查它是否是存在的元素?
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
Run Code Online (Sandbox Code Playgroud) 代码在访问网页时会起作用,它会自动隐藏#OrderDeliveryAddressdiv.但我想知道这是正确的做法吗?
有没有办法检查.selectAddressListdiv/class 是否先存在,然后检查值?
$(document).ready(function() {
if ($(".selectAddressList").val() == "selectAddressBook") {
$("#OrderDeliveryAddress").hide();
}
});
Run Code Online (Sandbox Code Playgroud) 如何检查jQuery .each()函数是否找不到内容,然后将txt值更改为“默认”。
$(this).closest('.filter-select__dropdown-inner').find('> .button--checkbox.button--active').each(function(i) {
var val = $(this).val();
if (i == 0) {
txt = val;
} else {
txt = txt + ', ' + val;
}
});
Run Code Online (Sandbox Code Playgroud)
我已经尝试
if (i == null) {
txt = 'default';
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用