简体   繁体   中英

Internet Explorer 11 behaves differently on two machines

I'm running IE 11 on my machine. I have a webpage that uses getElementsByClassName. When I access the webpage on my machine the page is served up and it works fine (it executes getElementsByClassName). If I access the same webpage on a different machine also using IE and it can't find the method getElementsByClassName. If I save the code served up to the browser on the failing machine and access that, the saved page works fine (it executes elemmentsByClassName works). What is going on? How do I tell the browser to use the version of the DOM or javascript that has getElementsByClassName?

var list = document.getElementsByClassName("mrClickableRow");

Make sure that IE is using the correct Document Mode to render your page, if not, it can simulate and older DOM-API which can cause some problems.

Open your Developer Tools by hitting F12 in IE, or selecting it by the settings-menu. Then go to the Emulation-tab and look at the Document Mode-dropdown.

document.getElementsByClassName is available from IE 9, are you sure you integrate JS with <script type="text/javascript"> ?

PS Use <script> before the </body> (body closing tag)

For compatibility issues, you can use this function.

window.getElementsByClassName = function(node, classname) {
    var a = [];
    var re = new RegExp('(^| )' + classname + '( |$)');
    var els = node.getElementsByTagName("*");
    for (var i = 0, j = els.length; i < j; i++)
        if (re.test(els[i].className)) a.push(els[i]);
    return a;
}

if (!document.getElementsByClassName) {
    document.getElementsByClassName = function(className) {
        return window.getElementsByClassName(document.body.parentNode, className);
    };
}

But be aware that this function and the built-in function are slightly different: When you use the built-in document.getElementsByClassName function, it returns an HTMLCollection . If you then delete the class of an element contained in the HTMLCollection, it is also removed from the HTMLCollection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM