我们可以通过呢 jQuery 的 length 属性判断 id 是否存在:
也可以写成插件形式,如下,插件可以判断元素是否存在
实例
$.fn.exists = function(callback) {
var args = [].slice.call(arguments, 1);
callback.call(this, args);
}
}; // 使用方法
$(‘div.test‘).exists(function() {
this.append(‘
});
var args = [].slice.call(arguments, 1);
if (this.length) {
callback.call(this, args);
}
return this;
}; // 使用方法
$(‘div.test‘).exists(function() {
this.append(‘
存在!
‘);});
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
评论(0)