javascript JQuery 删除 div 在 Internet Explorer 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9443470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery remove div not working in Internet Explorer
提问by Adry
I'm not really a javascript/jquery coder, I have a very simple code to remove a div that's not working in IE
我不是真正的 javascript/jquery 编码器,我有一个非常简单的代码来删除在 IE 中不起作用的 div
I'm using this in a Joomla page, so I call it like this:
我在 Joomla 页面中使用它,所以我这样称呼它:
$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 :)
FireFox 和 Chrome(一如既往)没问题。有人可以指出我的错误吗?非常感谢 :)
EDITED ************
编辑*** *** *** ***
I've tryied also with this code no jquery, but always not working in IE (9)
我也试过这个代码没有 jquery,但总是不能在 IE 中工作 (9)
<script>
setTimeout('yourFunction();', 5000);
function yourFunction(){
var div = document.getElementById("yourDiv");
div.parentNode.removeChild(div);
}
</script>
回答by The Alpha
回答by Paulo Rodrigues
Maybe this addScript doesn't work. Check if jQuery is loaded:
也许这个 addScript 不起作用。检查 jQuery 是否已加载:
alert(typeof($));
This alert message would return functionor undefined.
此警报消息将返回function或undefined。
回答by user1081874
I had this same issue and eventually realized I was using .append to a div and mistakenly also adding my own closing div.
我遇到了同样的问题,最终意识到我正在使用 .append 到一个 div 并错误地添加了我自己的结束 div。
IE is very picky about having elements nested properly.
IE 对正确嵌套元素非常挑剔。
回答by Tom
I tested this in internet explorer and firefox, works in both.
我在 Internet Explorer 和 Firefox 中对此进行了测试,两者都适用。
<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>