如何从调用插件的集合(而不是从 DOM 本身)中删除没有 id
属性的元素?
<span id="id737293" class="attach"></span>
<div class="attach"></span>
jQuery 调用和插件:
$('.attach').attach();
(function($) {
$.fn.attach = function(options) {
// Remove elements (without id) on which the plugin was invoked
var valid = ???;
// Loop each valid element (with id attribute) and process
valid.each(function() {
});
return this; // Maintain chainability
};
})(jQuery);
最佳答案
使用.filter删除没有 ID 的元素。
(function($) {
$.fn.attach = function(options) {
// Remove elements (without id) on which the plugin was invoked
var valid = this.filter(function() { return !!this.id; });
// Loop each valid element (with id attribute) and process
valid.each(function() {
});
return this; // Maintain chainability
};
})(jQuery);
$('.attach').attach();
或var valid = this.not(':not([id])');
- http://jsfiddle.net/5Kn9W/1/
关于jquery - 从 jQuery 选择中删除没有 id 的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9034519/