我编写了以下代码以在 5 秒后删除具有 rc-anchor-pt 类的元素(如果它存在于 DOM 中),
checkContainer();
counter = 1;
function checkContainer () {
alert("checkContainer");
$('.rc-anchor-pt').remove();
$('.rc-anchor-logo-portrait').append('<a href=\"http://www.un.org/en/aboutun/privacy/\" target=\"_blank\">Privacy & Terms</a>');
if($('.rc-anchor-pt').is(':visible')){ //if the container is visible on the page
var privacy = $('.rc-anchor-pt').find('a');
} else {
if (counter === 1)
{
setTimeout(checkContainer, 5000); //wait 50000 ms, then try again
counter++;
}
}
}
但是下面的行没有从 DOM 中删除元素。你能告诉我是什么原因吗?在此先感谢。我在 document.ready 中运行该元素仅存在于页面中 -
$('.rc-anchor-pt').remove();
最佳答案
我不太确定你想用你的代码来完成什么。您在问题中声明您希望在 5 秒后从 DOM 中删除一个元素......您应该能够使用以下代码完成此操作:
$('.rc-anchor-logo-portrait').append('<br><a href=\"http://www.un.org/en/aboutun/privacy/\" target=\"_blank\">Privacy & Terms</a>');
setTimeout(function(){
$('.rc-anchor-pt').remove();
}, 5000);
您的代码布局方式,
rc-anchor-pt
上课会从不可见。到时候就真的没有目的了。如果你想要 append
函数也可以在 5 秒后运行,只需将其放在 setTimeout 函数中即可。这是一个 fiddle :https://jsfiddle.net/1399u65t/3/
关于javascript - Dom 操作不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40246214/