javascript jQuery 在加载时按 ID 删除 div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17477267/
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 by ID on load
提问by Nick Moreau
I'm trying to remove an entire div on page load. Currently I'm trying to use:
我正在尝试在页面加载时删除整个 div。目前我正在尝试使用:
$(document).ready(function(){
$("#removeme").remove();
});
with the HTML
使用 HTML
<div id="removeme">If JS enabled remove this!</div>
I've been searching for hours, using many variations including getElementById. No luck.
我已经搜索了几个小时,使用了许多变体,包括 getElementById。没有运气。
Javascript/jQuery aren't languages I use often.
Javascript/jQuery 不是我经常使用的语言。
All help is appreciated. Thanks!
感谢所有帮助。谢谢!
Edit: Fixed, page I was adding it to was having some issues (another dev built it). I put the code into an existing JS file instead and it works. Thank you all.
编辑:已修复,我将其添加到的页面存在一些问题(另一个开发人员构建了它)。我将代码放入现有的 JS 文件中,它可以工作。谢谢你们。
回答by kunal18
$('#remove').html('');
or
或者
$('#remove').empty();
That will remove that text if JS is enabled.
如果启用了 JS,这将删除该文本。
回答by Dany Caissy
The code you are showing in your question works, as proven by @barmar :
正如@barmar 所证明的那样,您在问题中显示的代码有效:
"http://jsfiddle.net/barmar/epsZe/"
If it doesn't work for you, it is most likely because you have an error in your Javascript beforeyou reach the code displayed here. Also, ensure that you have included Jquery before-hand.
如果它对您不起作用,很可能是因为在您到达此处显示的代码之前,您的 Javascript 中存在错误。另外,请确保您事先包含了 Jquery。
To find where previous errors could have happened, you can look inside your Javascript console. Here is a question where it is described where to find your console on most browsers :
要查找以前的错误可能发生的位置,您可以查看 Javascript 控制台内部。这是一个问题,其中描述了在大多数浏览器上在哪里可以找到您的控制台:
What is console.log and how do I use it?
If you don't want to use the console, you can put alerts in your Javascript code until you find where they stop displaying.
如果您不想使用控制台,您可以在您的 Javascript 代码中放置警报,直到您找到它们停止显示的位置。
回答by user
try this
$("#removeme").replaceWith("");
试试这个
$("#removeme").replaceWith("");