如何从调用插件的集合中(而不是从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();


http://jsfiddle.net/5Kn9W/2/

var valid = this.not(':not([id])');-http://jsfiddle.net/5Kn9W/1/

关于jquery - 从jQuery选择中删除没有ID的元素?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9034519/

10-11 13:37