How to find an element that does not have an identifier, name, class, attribute, etc.?

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

<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>
+4
source share
5 answers

Thanks @adeneo for the info, this.attributesalways returning a value. That way I will clear the answer.

How to select elements that are “bare” (which have no attributes) using jQuery:

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

The method .text()is just a placeholder, in which case everything can be applied.

What the hell is simple jsFiddle here.

+5
source

attributes:

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

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

+4

, id, , , - , , .

, HTML, , HTML- , HTML-.

+2

.

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

attributes.length == 0 http://jsfiddle.net/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);
    }
}
0

Source: https://habr.com/ru/post/1534716/


All Articles