为什么我的代码在 IE11 中不起作用

why my code don't working in IE11

Internet Explorer 11 中,当我在按钮外部单击时,子菜单不会隐藏,但在 Google 中可以正常工作 ChromeFirefox.

http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onclick_dropdown

<!DOCTYPE html>
<html>
    <head>
        <style>
        .dropbtn {
            background-color: #4CAF50;
            color: white;
            padding: 16px;
            font-size: 16px;
            border: none;
            cursor: pointer;
        }

        .dropbtn:hover, .dropbtn:focus {
            background-color: #3e8e41;
        }

        .dropdown {
            position: relative;
            display: inline-block;
        }

        .dropdown-content {
            display: none;
            position: absolute;
            background-color: #f9f9f9;
            min-width: 160px;
            overflow: auto;
            box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
        }

        .dropdown-content a {
            color: black;
            padding: 12px 16px;
            text-decoration: none;
            display: block;
        }

        .dropdown-content a:hover {background-color: #f1f1f1}

        .show {display:block;}
        </style>
    </head>
    <body>
        <h2>Clickable Dropdown</h2>
        <p>Click on the button to open the dropdown menu.</p>
        <div class="dropdown">
            <button id="myBtn" class="dropbtn">Dropdown</button>
            <div id="myDropdown" class="dropdown-content">
                <a href="#home">Home</a>
                <a href="#about">About</a>
                <a href="#contact">Contact</a>
            </div>
        </div>
        <script>
            // Get the button, and when the user clicks on it, execute myFunction
            document.getElementById("myBtn").onclick = function() {myFunction()};
            // myFunction toggles between adding and removing the show class, which is used to hide and show the dropdown content
            function myFunction() {
                document.getElementById("myDropdown").classList.toggle("show");
            }

            // Close the dropdown if the user clicks outside of it
            window.onclick = function(event) {
            if (!event.target.matches('.dropbtn')) {
                var dropdowns = document.getElementsByClassName("dropdown-content");
                var i;
                for (i = 0; i < dropdowns.length; i++) {
                    var openDropdown = dropdowns[i];
                    if (openDropdown.classList.contains('show')) {
                        openDropdown.classList.remove('show');
                    }
                }
            }
        }
    </script>
</body>
</html>

event.target.matches 在 Internet Explorer 中不存在。 Element.matches() 中的浏览器兼容性 table 表示从 IE 9 开始可以使用此方法,但使用非标准名称 msMatchesSelector.

所以尝试:

window.onclick = function(event) {
    matches = event.target.matches ? event.target.matches('.dropbtn') : event.target.msMatchesSelector('.dropbtn');
    if (!matches) {
        var dropdowns = document.getElementsByClassName("dropdown-content");
        var i;
        for (i = 0; i < dropdowns.length; i++) {
            var openDropdown = dropdowns[i];
            if (openDropdown.classList.contains('show')) {
                openDropdown.classList.remove('show');
            }
        }
    }
}

您可以使用 jQuery 库获取跨浏览器兼容的代码,该代码也适用于 IE。

在您的情况下,将您的 <script> 替换为以下代码:

<script src="//code.jquery.com/jquery-1.12.3.min.js"></script>
<script>
$(function(){
    $(document).click(function(event){
        if(!$(event.target).is('#myBtn')
        && !$(event.target).is('#myDropdown a')) {
            $('#myDropdown').hide();
        }
        if($(event.target).is('#myBtn')) {
            $('#myDropdown').toggle();
        }
    });
});
</script>