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

发布于 2024-12-28 02:13:02 字数 405 浏览 0 评论 0原文

我想从没有类名的 span 元素中提取文本。怎么办呢? 可以搜索具有特定类名的元素,但如何仅获取没有类名的元素?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

乜一 2025-01-04 02:13:02

迭代它们并检查我认为的类名。

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

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
    }
}
空心↖ 2025-01-04 02:13:02

可以进行更多优化..但这就是你可以做到这一点的方法..

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

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;
        }
征棹 2025-01-04 02:13:02

尽管我觉得它很烦人,我还是会给出一个 jQuery 答案(因为 mrtsherman 用一个真实答案抢先一步):

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

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])')
会发光的星星闪亮亮i 2025-01-04 02:13:02

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

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

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

document.getElementsByTagName('span').querySelectorAll('span:not([class])');
叫思念不要吵 2025-01-04 02:13:02

在我的朋友投反对票后,我在核心 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>

这也可以使用 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>

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.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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文