JQuery remove div not working in Internet Explorer

2019-05-07 13:04发布

问题:

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>

回答1:

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

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



回答2:

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

alert(typeof($));

This alert message would return function or undefined.



回答3:

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>


回答4:

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.