简体   繁体   中英

Javascript function is undefined in IE11

I wrote a sample html page to show a popup div, it's working in firefox, but not in IE. it said the function is undefined.

Here is my page:
and the error message is "'show_popup_div' is undefined"

<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=unicode" />

    <script type="text/javascript">

        function show_popup_div() {
            var imageDiv=document.getElementById("image_div");
            var switchA=document.getElementById("switch_a");
            imageDiv.style.display='block';
        }

        async function hide_popup_div() {
            var imageDiv=document.getElementById("image_div");
            await sleep(5000);
            imageDiv.style.display='none';
        }          

        function sleep(ms) {
            return new Promise(resolve => setTimeout(resolve, ms));
        }

    </script>
</head>
<body>

    <a id="switch_a" onmousemove="show_popup_div()" onmouseout="hide_popup_div()">click me to open a image</a>
    <div id="image_div">
        <img id="image" src="http://www.rd.com/wp-content/uploads/sites/2/2016/02/06-train-cat-shake-hands.jpg" usemap="#map1"/>

    </div>

</body>

How can i fix this? Thank you.

我相信,如果没有Jaromanda X提到的Javascript库,Promise函数将与IE不兼容。https ://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise

You don't need JavaScript for this effect, you can use pure CSS using the :hover pseudo-class and the + adjacent-element selector:

#image_div {
    display: none;
}

#switch_a:hover + #image_div {
    display: block;
}

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