gre*_*emo 2 jquery jquery-plugins jquery-selectors
如何id从调用插件的集合(而不是从DOM本身)中删除没有属性的元素?
<span id="id737293" class="attach"></span>
<div class="attach"></span>
Run Code Online (Sandbox Code Playgroud)
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);
Run Code Online (Sandbox Code Playgroud)
使用.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();
Run Code Online (Sandbox Code Playgroud)
或var valid = this.not(':not([id])');- http://jsfiddle.net/5Kn9W/1/