JQuery remove div not working in Internet Explorer

2019-05-07 12:50发布

I'm not really a javascript/jquery coder, I have a very simple code to remove a div that's not working in IE

I'm using this in a Joomla page, so I call it like this:

$document->addScript("http://code.jquery.com/jquery-latest.js");//adiciona jquery

And than, in the body document:

<script>
    setTimeout(function() {
        $("#yourDiv").remove();
    }, 50000);
</script>

FireFox and Chrome are (as always) ok. Can someone point out my mistake please? Thanks a lot :)

EDITED ************

I've tryied also with this code no jquery, but always not working in IE (9)

<script>
setTimeout('yourFunction();', 5000);
function yourFunction(){
var div = document.getElementById("yourDiv");
div.parentNode.removeChild(div);
}
</script>

4条回答
闹够了就滚
2楼-- · 2019-05-07 12:52
<script>
$(document).ready(function(){
    setTimeout(function() {
        $("#yourDiv").remove();
    }, 50000);
});
</script>

Check this fiddle, it's working in ie too.

查看更多
再贱就再见
3楼-- · 2019-05-07 12:59

Maybe this addScript doesn't work. Check if jQuery is loaded:

alert(typeof($));

This alert message would return function or undefined.

查看更多
再贱就再见
4楼-- · 2019-05-07 13:11

I had this same issue and eventually realized I was using .append to a div and mistakenly also adding my own closing div.

IE is very picky about having elements nested properly.

查看更多
乱世女痞
5楼-- · 2019-05-07 13:14

I tested this in internet explorer and firefox, works in both.

<html>
<head>
<script type="text/javascript">
    var p = {
        onload: function() {
            setTimeout(
                function() {
                    var div = document.getElementById("myDiv");
                    div.parentNode.removeChild(div);
                },
                3000
            );
        }
    }
</script>
</head>
    <body onload="p.onload()">
    <div id="myDiv" style="height: 50px; width: 50px ;background-color: grey;"></div>
    </body>
</html>
查看更多
登录 后发表回答