简体   繁体   English

如何选择没有属性的元素

[英]How to select an element that has no attributes

So i have couple of spans within a div and most of them have classes or other attributes. 所以我在div中有几个跨度,大多数都有类或其他属性。

<span class="one" attribute="test"></span>
<span></span> /* This is what i need to select */
<span class="three" attribute="test"></span>
<span class="four" attribute="test"></span>
<span></span>

I would like to select the very first span that has no attributes but also make sure that the last span that has no attributes wont be selected. 我想选择没有属性的第一个跨度,但也要确保不选择没有属性的最后一个跨度。

Also, the element with class one will sometimes appear and sometimes wont so i can't select it with a number like: [1] . 另外,与类中的元件one有时会出现,有时不会所以我不能与多个像选定: [1]

What would be the best approach? 什么是最好的方法?

You can check to see how many attributes an element has by checking the length of its attributes property: 您可以通过检查其attributes属性的长度来检查元素的属性数量:

 const div = document.querySelector('div'); const spanWithNoAttributes = [...div.children] .find(span => span.attributes.length === 0); spanWithNoAttributes.textContent = 'THIS ONE HERE'; 
 <div> <span class="one" attribute="test">one</span> <span>two</span> <span class="three" attribute="test">three</span> <span class="four" attribute="test">four</span> <span>five</span> </div> 

You should also fix up your HTML - end tags should have a slash before the tag name, not after. 您还应该修复HTML - 结束标记应该在标记名称之前有一个斜杠,而不是之后。

If the order of spans is the same, and if the span never gets attributes, then you could just use the nth-child selector. 如果跨度的顺序相同,并且如果跨度永远不会获得属性,那么您可以使用第nth-child选择器。

With jQuery... 用jQuery ......

$("span:nth-child(2)").blah.blah;

If you need to make sure there are no attributes at all, you'll need to select the elements by tag (or globally) and perform a filter. 如果您需要确保根本没有属性,则需要按标签(或全局)选择元素并执行过滤。

 var spans = Array.from(document.querySelectorAll("span")); var noattrs = spans.find(s => !s.attributes.length); alert(noattrs.outerHTML); 
 <span class="one" attribute="test"></span> <span></span> /* This is what i need to select */ <span class="three" attribute="test">/<span> <span class="four" attribute="test"></span> <span></span> 

However, be aware that there are legacy browsers that may automatically set some attributes in the .attributes list, so this may not work in all cases. 但是,请注意,有些旧版浏览器可能会自动在.attributes列表中设置某些属性,因此这可能并不适用于所有情况。


If you only need to prevent those with a specific attribute or set of attributes, you can use the :not() selector with the "has attribute" selector. 如果您只需要阻止具有特定属性或属性集的那些,则可以将:not()选择器与“has attribute”选择器一起使用。

 var noattr = document.querySelector("span:not([attribute])"); alert(noattr.outerHTML); 
 <span class="one" attribute="test"></span> <span></span> /* This is what i need to select */ <span class="three" attribute="test">/<span> <span class="four" attribute="test"></span> <span></span> 

If there are other specific ones to prevent, you can add more :not() selectors. 如果还有其他特定要防止,可以添加更多:not()选择器。 "span:not([attribute]):not([id]):not([class])"

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

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