简体   繁体   English

你怎么知道HTML元素是否有一个普通的Javascript类?

[英]How do you find out if an HTML element has a certain class with plain Javascript?

Say I have this html element 说我有这个html元素

<div id="hello" class="hello option john">Hello</div>
<div id="hello" class="hello john">Hello</div>

. Now I select the element with javascript by it's Id. 现在我通过它的Id选择带有javascript的元素。 How would I do an equivalent of if($('hello').hasClass('option')){//do stuff} (jQuery) except in plain Javascript? 除了简单的Javascript之外,我如何做一个等效的if($('hello').hasClass('option')){//do stuff} (jQuery)?

if(document.getElementById("hello").className.split(" ").indexOf("option") > -1);

That'll do it. 那就行了。

EDIT: demo 编辑: 演示

or as a prototyped function: 或作为原型功能:

HTMLElement.prototype.hasClass = function(c){
    return this.className.split(" ").indexOf(c) > -1
}

and

document.getElementById("hello").hasClass("option")

Update 2015: 2015年更新:

In modern browsers including IE 10 you can write: 在包括IE 10的现代浏览器中,您可以编写:

document.getElementById("hello").classList.contains("option")

See the reference: https://developer.mozilla.org/en-US/docs/Web/API/Element/classList 参见参考: https//developer.mozilla.org/en-US/docs/Web/API/Element/classList

It's supported in all major browsers (ref: http://caniuse.com/#search=classlist ) 所有主流浏览器都支持它(参考: http//caniuse.com/#search=classlist

If your targets are modern browsers, you can use querySelector() . 如果您的目标是现代浏览器,则可以使用querySelector() Otherwise, you can play with className property. 否则,您可以使用className属性。 ;) ;)

Is somehow simple: 有点简单:

var elHasClass = function(element, classToTest) {
    var pattern = new RegExp("(^| )" + classToTest + "( |$)");
    return pattern.test(element.className) ? true : false;
};

Now you can do: 现在你可以这样做:

if( elHasClass(myEl, 'someCssClass') ) { ... do you stuff here ... }

暂无
暂无

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

相关问题 在Plain / Pure Javascript中,如何找出元素对象有哪些可用的方法或属性? - In Plain/Pure Javascript, how do you find out what available methods or properties an element object has? 如何在具有特定样式的 Javascript 数组中找到 HTML 元素? - How to Find an HTML Element in a Javascript Array That Has a Certain Styling? 您如何仅选择javascript中元素具有的第二类? - How do you only select the second class an element has in javascript? 如何在普通Javascript中删除和隐藏HTML元素? - How do you remove and hide HTML elements in plain Javascript? 如何使用纯Javascript将未知HTML括在div中? - How do you enclose unknown HTML in a div with plain Javascript? 如何找到在body元素之后的每个子元素,并获取其中包含特定类的元素的html - How do I find each child that comes after the body element, and get the html of the element with a certain class within it 如何在纯JavaScript中找到HTML节点的匹配子树 - How do I find matching subtree of HTML nodes in plain JavaScript 使用 javascript,如何从元素中的某些 class 名称中跳过或删除 e.stopPropagation()? - Using javascript, how do you skip, or remove, e.stopPropagation() from certain class names in an element? 使用纯JavaScript将类添加到html元素时侦听事件 - Listen to an event when a class has been added to an html element using plain javascript 如何确定Javascript变量中是否包含某些字符 - How to find out if a Javascript variable has certain characters in it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM