gpt4 book ai didi

javascript - 使用 Internet Explorer 查看页面时删除元素

转载 作者:行者123 更新时间:2023-11-30 15:01:19 31 4
gpt4 key购买 nike

我有如下所示的以​​下代码:

<div class="timer" id="timer"><img src="http://i.imgur.com/87XaOWA.png"><p class="close-message" id="close-message"></p></div>

现在,当在 Internet Explorer 中查看页面时,我希望删除 div

IE doesn't support the .remove() function我找到了以下解决方案来规避问题 here .我还发现了以下 fiddle可以检测正在使用哪个浏览器查看页面。

我已经尝试了以下两个 if 语句来删除在 IE 中查看时的 div 标签,但无济于事:

// Internet Explorer 6-11
var isIE = /*@cc_on!@*/false || !!document.documentMode

if (isIE = false) {
jQuery("#timer").eq(i).remove();
}

// Internet Explorer 6-11
var isIE = /*@cc_on!@*/false || !!document.documentMode

if (isIE = false) {
var node = document.getElementsById('timer')[i];
node.parentNode.removeChild(node);
}

我做错了什么?这两个组件单独使用时工作正常,但当我尝试将它们一起使用时,它们就不起作用了。

最佳答案

您的第一个错误是:

if (isIE = false) {

您需要使用双等号进行比较,在您的情况下应该是:

if (isIE == true) {

第二个错误是:

document.getElementsById('timer')[i]

将其更改为:

document.getElementById('timer')

var isIE = /*@cc_on!@*/false || !!document.documentMode;

if (isIE == true) {
var node = document.getElementById('timer');
node.parentNode.removeChild(node);
}

//
// in jQuery: remove: .eq(i)....
//
if (isIE == true) {
jQuery("#timer").remove();
}
<div class="timer" id="timer"><img src="http://i.imgur.com/87XaOWA.png"><p class="close-message" id="close-message"></p></div>

关于javascript - 使用 Internet Explorer 查看页面时删除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46493849/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com