简体   繁体   English

如何在javascript中获取没有类名的元素

[英]how to get elements with no class name in javascript

I want to extract text from span elements with no class name. 我想从没有类名的span元素中提取文本。 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>

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;
        }

使用Element.querySelectorAll ,您可以使用以下代码来实现。

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

尽管我发现它很烦人,但我会给出一个jQuery答案(因为mrtsherman用真实的答案击败了我)

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

After downvote of my friend I have added code in core java script here: 在我朋友的投票之后,我在此处的核心java脚本中添加了代码:

<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: 这也可以用jQuery完成:

<html>
<head>
<style>
span{display:block;}
</style>
<script src="http://ajax.googleapis.com/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>

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

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