最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How do I find an element that does not have an id, name, class, attribute, etc.? - Stack Overflow

matteradmin7PV0评论

I want to find (probably using jquery) an element that is empty/naked/etc. For example I want to find all the span elements that do not have an id, name, class, or any other attribute whatsoever.

<html>
<head></head>
<body>
    <span> found </span>

    <span id="foo"> not found </span>

    <span name="foo"> not found </span>

    <span class="foo"> not found </span>

    <span style="width:foo"> not found </span>

    <span> found </span>

    <span foo="bar"> not found </span>
</body>
</html>

I want to find (probably using jquery) an element that is empty/naked/etc. For example I want to find all the span elements that do not have an id, name, class, or any other attribute whatsoever.

<html>
<head></head>
<body>
    <span> found </span>

    <span id="foo"> not found </span>

    <span name="foo"> not found </span>

    <span class="foo"> not found </span>

    <span style="width:foo"> not found </span>

    <span> found </span>

    <span foo="bar"> not found </span>
</body>
</html>
Share Improve this question edited Apr 2, 2014 at 20:40 irrational asked Apr 2, 2014 at 20:29 irrationalirrational 7999 silver badges29 bronze badges 1
  • 2 This question is inplete without an exact piece of HTML that you want to find a particular element in. There is no generic answer to this question - any answer has to be taylored to a specific target in a specific piece of HTML. Your example so far is not plete HTML (no closing tags) so I assume it's just a demo, not the actual HTML you want to find something in. – jfriend00 Commented Apr 2, 2014 at 20:36
Add a ment  | 

5 Answers 5

Reset to default 5

Thanks @adeneo for the information, this.attributes always returns a value. As such, I'll clean up the answer.

How to select elements that are "naked" (that do not have any attributes) using jQuery:

$('span').filter(function(){
    return (this.attributes.length === 0);
}).text();

The .text() method is just a placeholder, anything can be applied at that point.

What the hell, here's a simple jsFiddle.

Look for an empty attributes list:

var spans = $('span').filter(
  function() {
    return (this.attributes.length == 0);
});

Example: http://codepen.io/paulroub/pen/JjAia/

If an element doesn't have any identifying class, id, name or attribute, then the only way to find it is by its location in the hierarchy and its position in that hierarchy and, in some cases, by the attributes that it does NOT have or perhaps by the content that it has.

There is no generic answer to this question as it really depends upon your exact circumstances and exact HTML so folks can only help you more specifically ONLY if you provide the exact HTML and tell us which element you're looking for in that HTML.

Try this.

var elements=document.getElementsByTagName("span")
for(var v=0;v<elements.length;v++){
    if(elements[v].attributes.length==0){
    //your element without any attribute
    }
}

You just need to loop through the elements and find the elements with attributes.length == 0 http://jsfiddle/HjBGP/

var els = document.getElementsByTagName('span');

for(var i=0;i<els.length;i++){
    if(els[i].attributes.length == 0) {
       console.log(els[i].innerHTML);
    }
}
Post a comment

comment list (0)

  1. No comments so far