Html Javascript:检查类名是否存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16475704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 08:16:23  来源:igfitidea点击:

Javascript: Check if classname exists

javascripthtmlclassname

提问by user2276965

<div class="Trade">
    <div id="Offer">
        <div class="NoResults">No Result!</div>
        <div class="FooterPager"> <span id="Offer"><a disabled="disabled">First</a>&nbsp;<a disabled="disabled">Previous</a>&nbsp;<a disabled="disabled">Next</a>&nbsp;<a disabled="disabled">Last</a>&nbsp;</span>
        </div>
    </div>
</div>

Here is my javascript:

这是我的javascript:

function Check(){
return !(iframe.contentDocument.getElementById("Offer").firstElementChild.tagName.toLowerCase() == "table");
}

Is it possible to return a true or false value to check if the class "NoResult" exists? If so, how do I do it? You guys rocks. I can't change the HTML coding, only the javascript.

是否可以返回真值或假值来检查类“NoResult”是否存在?如果是这样,我该怎么做?你们摇滚。我无法更改 HTML 编码,只能更改 javascript。

回答by alex

Use classList.

使用classList.

var hasClass = element.classList.contains('some-class');

Further Reading(disclaimer: link to my own post).

进一步阅读(免责声明:链接到我自己的帖子)。

If not supported in your target platforms, then try...

如果您的目标平台不支持,请尝试...

var hasClass = (" " + element.className + " ").indexOf(" some-class ") > -1;

回答by Turnerj

In Javascript without using a library like jQuery, you could do it by:

在不使用像 jQuery 这样的库的 Javascript 中,您可以通过以下方式完成:

(' ' + MyElement.className + ' ').indexOf(' MyClassName ') != -1

This evaluates to true when "MyClassName" appears anywhere by itself inside the string as defined on the classNameproperty.

当“MyClassName”单独出现在className属性上定义的字符串内的任何位置时,它的计算结果为真。

In your specific case, something like:

在您的具体情况下,例如:

function Check()
{
    //Returns true when it exists
    return (' ' + iframe.contentDocument.getElementById('Offer').firstElementChild.className + ' ').indexOf(' NoResults ') != -1;
}

There was previously a mistake in my answer where it would incorrectly identify a partial match as pointed out in the comments. Basically, you need to consider in the check that the class name is whole. A neat way to do this (like the other answers show) is that if you spaces before and after both the entire classNameproperty and the class you are searching for, it will always find the whole class.

以前我的回答中有一个错误,它会错误地识别评论中指出的部分匹配。基本上,您需要在检查中考虑类名是否完整。这样做的一种巧妙方法(如其他答案所示)是,如果您在整个className属性和您正在搜索的类之前和之后都有空格,它将始终找到整个类。

While this will work, I recommend Alex's answeras while classListisn't available in every browser(<= IE9 and a few others), it is a neater solution to the problem.

虽然这会奏效,但我推荐Alex 的答案,因为虽然classList并非在每个浏览器(<= IE9 和其他一些浏览器)中都可用,但它是解决问题的更简洁的方法。

回答by Joseph Silber

if ( ~(' ' + element.className + ' ').indexOf(' NoResult ') ) {
    // here you go. You've got that class...
}

回答by Juan Sebastian Montoya Contrer

How to check if an element has a class for all browsers: JQuery 1.6 or lower

如何检查元素是否具有适用于所有浏览器的类:JQuery 1.6 或更低版本

    if($("#ElementId").attr('class').indexOf('ClassName') > -1)

JQuery 1.6 or higher

JQuery 1.6 或更高版本

    if($("#ElementId").prop('class').indexOf('ClassName') > -1)

Or, if you are not going to use IE at all

或者,如果您根本不打算使用 IE

    if($("#ElementId").prop('class').includes('ClassName'))