jquery - 从 jQuery 选择中删除没有 id 的元素?

标签 jquery jquery-plugins jquery-selectors

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

相关文章:

JQuery MagicLine 导航到下拉菜单

javascript - 选择不在容器 jquery 中的 child

javascript - 用javascript反向堆栈填充

javascript - 解决方案 Bootstrap 表单验证

javascript - jquery仅验证数字

javascript - jQuery.verto 不是构造函数

jquery - 在 Backbone View 的事件哈希中使用自定义 jquery 插件

javascript - 使用 Jquery 将鼠标悬停在元素的前 24 个子元素上

javascript - 按类或 ID 选择元素

javascript - 将值传递给隐藏字段并提交给 CodeIgniter