admin管理员组文章数量:1435859
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
4 Answers
Reset to default 3You 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";
}
}
本文标签: javascriptclassListcontains(39outputDiv39) not supported in IE 9 and older Stack Overflow
版权声明:本文标题:javascript - classList.contains('outputDiv') not supported in IE 9 and older, - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745575974a2664328.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论