admin管理员组

文章数量:1426810

I want to extract text from span elements with no class name. How can it be done? Elements with particular class name can be searched but how to get only the elements with no class name for them?

<span>0m2abBrL+RIHOEA+dZS+OqV3St+nJ/</span>
<wbr></wbr>
<span class="word_break"></span>
<span>zwq73Gfz8MQGB0yS++lfufSOV133huE</span>
<wbr></wbr>
<span class="word_break"></span>
vCB0s5D9w
<span class="text_exposed_hide">...</span>

I want to extract text from span elements with no class name. How can it be done? Elements with particular class name can be searched but how to get only the elements with no class name for them?

<span>0m2abBrL+RIHOEA+dZS+OqV3St+nJ/</span>
<wbr></wbr>
<span class="word_break"></span>
<span>zwq73Gfz8MQGB0yS++lfufSOV133huE</span>
<wbr></wbr>
<span class="word_break"></span>
vCB0s5D9w
<span class="text_exposed_hide">...</span>
Share Improve this question asked Jan 18, 2012 at 5:45 vaichidrewarvaichidrewar 9,63119 gold badges75 silver badges88 bronze badges 2
  • do you want this in pure javascript or jquery could be an option? – Shoaib Shaikh Commented Jan 18, 2012 at 5:49
  • Hi, I added code in java script also – Umesh Patil Commented Jan 18, 2012 at 6:21
Add a ment  | 

5 Answers 5

Reset to default 4

Iterate through them and check the classname I suppose.

var spans = document.getElementsByTagName('span');
for (var i = 0; i < spans.length; i++) {
    if (spans[i].className == '') {
        //span doesn't have a class
    }
}

more optimization could be done.. but this is how you can do this..

 function getElementsByNoClassName() {
            var node = document.getElementsByTagName("body")[0];
            var a = [];
            var els = node.getElementsByTagName("*");
            for (var i = 0, j = els.length; i < j; i++)
                if (els[i].className=='') a.push(els[i]);
            return a;
        }

Despite how annoying I find it, I'll give a jQuery answer (since mrtsherman beat me to the punch with a real answer):

$('span:not([class])')

With Element.querySelectorAll, you could use the following code to achieve that.

document.getElementsByTagName('span').querySelectorAll('span:not([class])');

After downvote of my friend I have added code in core java script here:

<html>
<head>
<style>
span{display:block;}
</style>
<script type="text/javascript">
function getElementsByNoClassName() {
            var node = document.getElementsByTagName("span");
            console.log(node.length);
            var a = [];         
            for (var i = 0, j = node.length; i < j; i++){
                   if (node[i].className=='') 
                    node[i].setAttribute("style","color:red;");            
                  }
 }

window.onload=getElementsByNoClassName;
</script>
</head>
<body>
Content with No class is colored in red.
<span>0m2abBrL+RIHOEA+dZS+OqV3St+nJ/</span>
<wbr></wbr>
<span class="word_break"></span>
<span>zwq73Gfz8MQGB0yS++lfufSOV133huE</span>
<wbr></wbr>
<span class="word_break"></span>
vCB0s5D9w
<span class="text_exposed_hide">...</span>
</body>
</html>

This can be done with jQuery also:

<html>
<head>
<style>
span{display:block;}
</style>
<script src="http://ajax.googleapis./ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){

$("span").filter(
      function(){
         return $(this).attr('class')==undefined
       }).css('color','red');
});
</script>
</head>
<body>
Content with No class is colored in red.
<span>0m2abBrL+RIHOEA+dZS+OqV3St+nJ/</span>
<wbr></wbr>
<span class="word_break"></span>
<span>zwq73Gfz8MQGB0yS++lfufSOV133huE</span>
<wbr></wbr>
<span class="word_break"></span>
vCB0s5D9w
<span class="text_exposed_hide">...</span>
</body>
</html>

本文标签: scriptinghow to get elements with no class name in javascriptStack Overflow