简体   繁体   English

如何 select HTML 元素上没有定义任何属性?

[英]How to select HTML elements which don't have any attributes defined on them?

I need to use jQuery to locate all DIV tags that have no attributes on them and apply a class to each.我需要使用 jQuery 来定位所有没有属性的 DIV 标签,并对每个标签应用 class。 Here's a sample HTML:这是一个示例 HTML:

<div id="sidebar">
  <div>Some text goes here</div>
  <div class="something">something goes here</div>
  <div>Another div with no attributes.</div>
</div>

So, I need to take that and turn it into this:所以,我需要把它变成这样:

<div id="sidebar">
  <div class="myClass">Some text goes here</div>
  <div class="something">something goes here</div>
  <div class="myClass">Another div with no attributes.</div>
</div>

How do you locate elements of type div that have no attributes via jQuery?如何通过 jQuery 定位没有属性的div类型元素? Thanks.谢谢。

Here you go:这里是 go:

$('div', '#sidebar').filter(function () {
    return this.attributes.length === 0;
})

Live demo: http://jsfiddle.net/phbU9/现场演示: http://jsfiddle.net/phbU9/

The attributes property returns a list of all attributes set on the element. attributes属性返回在元素上设置的所有属性的列表。 "Naked" elements have an empty attributes list. “裸”元素有一个空attributes列表。

Update: Be sure to read Tim's answer below which provides a solution for older versions of IE, since my own solution doesn't work in IE8 and below.更新:请务必阅读下面Tim 的回答,它为旧版本的 IE 提供了解决方案,因为我自己的解决方案在 IE8 及以下版本中不起作用。

@Šime's answer is close but doesn't work in IE 6, 7 or 8, where an element's attributes collection has an entry for every possible attribute, not just those specified in the HTML. @Šime 的答案很接近,但在 IE 6、7 或 8 中不起作用,其中元素的attributes集合具有每个可能属性的条目,而不仅仅是 HTML 中指定的那些。 You can get round this by checking each attribute object's specified property.您可以通过检查每个属性对象的specified属性来解决这个问题。

Live demo: http://jsfiddle.net/timdown/6MqmK/1/现场演示: http://jsfiddle.net/timdown/6MqmK/1/

Code:代码:

$("div").filter(function() {
    var attrs = this.attributes, attrCount = attrs.length;
    if (attrCount == 0) {
        return true;
    } else {
        for (var i = 0; i < attrCount; ++i) {
            if (attrs[i].specified) {
                return false;
            }
        }
        return true;
    }
});

To expound upon Tim Down's answer, I recommend checking that the attrs var not null special cases where the html has comment tags, etc.为了解释 Tim Down 的回答,我建议检查 attrs var 不是 null 特殊情况,其中 html 有评论标签等。

You need to give some sort of selector, in this case Ive used your side bar but it can be anything.您需要提供某种选择器,在这种情况下,我使用了您的侧栏,但它可以是任何东西。 Then get the children that have no class attribute and add a new class.然后获取没有 class 属性的孩子,并添加一个新的 class。 See JSFiddle for the example:有关示例,请参见 JSFiddle:

http://jsfiddle.net/HenryGarle/q3x5W/ http://jsfiddle.net/HenryGarle/q3x5W/

  $("#sidebar").children('div:not([class])').addClass('newClass');

So this would return the 2 elements with no class tag and leave the sidebar and div with the class completely unaffected.因此,这将返回没有 class 标记的 2 个元素,并使 class 的侧边栏和 div 完全不受影响。

You could use a combination of jQuery's has attribute selector and the not selector .您可以使用 jQuery 的has 属性选择器not 选择器的组合。 For example:例如:

$('div:not([class], [id])').addClass('myClass');

jsFiddle demonstrating this jsFiddle 演示了这一点

With this approach, you need to explicitly specify the attributes to check the presence of.使用这种方法,您需要显式指定要检查存在的属性。 Sime's solution would apply the class to divs that do not have any attributes at all. Sime 的解决方案会将 class 应用于根本没有任何属性的 div。

try $('div:not([class])').addClass('myClass');试试 $('div:not([class])').addClass('myClass'); it is a general approach because the class will apply to all the div that have no class这是一种通用方法,因为 class 将适用于所有没有 class 的 div

$('#sidebar div')` or more general `$('div'); //returns collections of divs

to answer the question:回答这个问题:

$('#sidebar div').addClass('myClass');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM