Check if an element contains a class in JavaScript?

后端 未结 27 2228
面向向阳花
面向向阳花 2020-11-22 09:36

Using plain JavaScript (not jQuery), Is there any way to check if an element contains a class?

Currently, I\'m doing this:

相关标签:
27条回答
  • 2020-11-22 10:16

    Just to add to the answer for people trying to find class names within inline SVG elements.

    Change the hasCLass() function to:

    function hasClass(element, cls) {
        return (' ' + element.getAttribute('class') + ' ').indexOf(' ' + cls + ' ') > -1;
      }
    

    Instead of using the className property you'll need to use the getAttribute() method to grab the class name.

    0 讨论(0)
  • 2020-11-22 10:17

    This is a little old, but maybe someone will find my solution helpfull:

    // Fix IE's indexOf Array
    if (!Array.prototype.indexOf) {
        Array.prototype.indexOf = function (searchElement) {
            if (this == null) throw new TypeError();
            var t = Object(this);
            var len = t.length >>> 0;
            if (len === 0) return -1;
            var n = 0;
            if (arguments.length > 0) {
                n = Number(arguments[1]);
                if (n != n) n = 0;
                else if (n != 0 && n != Infinity && n != -Infinity) n = (n > 0 || -1) * Math.floor(Math.abs(n));
            }
            if (n >= len) return -1;
            var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
            for (; k < len; k++) if (k in t && t[k] === searchElement) return k;
            return -1;
        }
    }
    // add hasClass support
    if (!Element.prototype.hasClass) {
        Element.prototype.hasClass = function (classname) {
            if (this == null) throw new TypeError();
            return this.className.split(' ').indexOf(classname) === -1 ? false : true;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 10:17

    I think that perfect solution will be this

    if ($(this).hasClass("your_Class")) 
        alert("positive");            
    else              
        alert("Negative");
    
    0 讨论(0)
  • 2020-11-22 10:19

    I would Poly fill the classList functionality and use the new syntax. This way newer browser will use the new implementation (which is much faster) and only old browsers will take the performance hit from the code.

    https://github.com/remy/polyfills/blob/master/classList.js

    0 讨论(0)
  • 2020-11-22 10:19

    in which element is currently the class '.bar' ? Here is another solution but it's up to you.

    var reg = /Image/g, // regexp for an image element
    query = document.querySelector('.bar'); // returns [object HTMLImageElement]
    query += this.toString(); // turns object into a string
    
    if (query.match(reg)) { // checks if it matches
      alert('the class .bar is attached to the following Element:\n' + query);
    }
    

    jsfiddle demo

    Of course this is only a lookup for 1 simple element <img>(/Image/g) but you can put all in an array like <li> is /LI/g, <ul> = /UL/g etc.

    0 讨论(0)
  • 2020-11-22 10:20

    Using the classList is also ideal

    HTML

    <div id="box" class="myClass"></div>
    

    JavaScript

    const element = document.querySelector("#box");
    
    element.classList.contains("myClass");
    
    0 讨论(0)
提交回复
热议问题