get element by classname for IE11

≡放荡痞女 提交于 2019-12-11 19:52:24

问题


I've an issue with queryselectorall and IE11. It works on IE10 and firefox but with last cheet Do you have a solution for having an element by his classname?

if(document.querySelectorAll(".classname"))
{
    alert('ici');
    document.querySelectorAll(".classname").style.display = "none";
}

The alert and display none is working on everything except Internet Explorer 11

thanks


回答1:


http://jsfiddle.net/r3XCd/12/

if (typeof document.querySelectorAll !== undefined) {
    if (document.querySelectorAll(".classname").length > 0) {
      var element = document.querySelectorAll(".classname");
      element[0].style.display="none";
    }
}

For multiple elements: http://jsfiddle.net/r3XCd/19/

if (typeof document.querySelectorAll !== undefined) {
    if (document.querySelectorAll(".classname").length > 0) {
      var elements = document.querySelectorAll(".classname");
      for (var i=0;i<elements.length;i++) {
        elements[i].style.backgroundColor ='red';
       }
    }
}


来源:https://stackoverflow.com/questions/22882305/get-element-by-classname-for-ie11

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!