最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - classList.contains('outputDiv') not supported in IE 9 and older, - Stack Overflow

matteradmin9PV0评论

classList.contains('outputDiv') not supported in IE 9 and older, is there any alternative for that?

My code is as below:

function toggleclass() {

    var elems = document.getElementsByClassName("outputDivs");
    for (var i = 0; i < elems.length; ++i) {
        if (elems[i].classList.contains("outputDivsDesigned"))
            elems[i].className = "outputDivs";

        else
            elems[i].className += " outputDivsDesigned";
    }
}

Thanks for any help.

classList.contains('outputDiv') not supported in IE 9 and older, is there any alternative for that?

My code is as below:

function toggleclass() {

    var elems = document.getElementsByClassName("outputDivs");
    for (var i = 0; i < elems.length; ++i) {
        if (elems[i].classList.contains("outputDivsDesigned"))
            elems[i].className = "outputDivs";

        else
            elems[i].className += " outputDivsDesigned";
    }
}

Thanks for any help.

Share Improve this question edited Sep 3, 2013 at 4:56 Eugene Loy 12.5k8 gold badges57 silver badges79 bronze badges asked Sep 3, 2013 at 4:28 Durgaprasad KatariDurgaprasad Katari 111 silver badge2 bronze badges 1
  • There's a shim for older browsers (down to IE8) on MDN - developer.mozilla/en-US/docs/Web/API/… – Phil Commented Sep 3, 2013 at 6:43
Add a ment  | 

4 Answers 4

Reset to default 3

You can try this https://github./eligrey/classList.js

or just use className, and search the returned string.

I just wrote this, perhaps it might be helpful?

http://www.tjcafferkey.me/ie9-classname-containsclass-alternative/

It basically explains this simple alternative you could use:

function containsClass(yourObj, yourClass) {
    if(!yourObj || typeof yourClass !== 'string') {
        return false;
    } else if(yourObj.className && yourObj.className.trim().split(/\s+/gi).indexOf(yourClass) > -1) {
        return true;
    } else {
        return false;
    }
}

You would then run the function like this

var isFeaturedPost = containsClass(obj, 'featured-post-class');

Edit: I've updated the function to check yourObj exists, and that yourClass is a string.

Not sure if it will work as have not tested to be working for crossbrowser you can give a try to:-

  classList.indexOf("outputDivsDesigned") != -1

If all you need is .contains() functionality, search for the substring inside of the className, i.e. elems[i].className.indexOf("outputDivsDesigned") > -1.

So your function would look like this:

function toggleclass() {    
    var elems = document.getElementsByClassName("outputDivs");
    for (var i = 0; i < elems.length; ++i) {
        if (elems[i].className.indexOf("outputDivsDesigned") > -1)
            elems[i].className = "outputDivs";    
        else
            elems[i].className += " outputDivsDesigned";
    }
}

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far