最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery remove div by ID on load - Stack Overflow

programmeradmin6浏览0评论

I'm trying to remove an entire div on page load. Currently I'm trying to use:

$(document).ready(function(){    
    $("#removeme").remove();
});

with the HTML

<div id="removeme">If JS enabled remove this!</div>

I've been searching for hours, using many variations including getElementById. No luck.

Javascript/jQuery aren't languages I use often.

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.

I'm trying to remove an entire div on page load. Currently I'm trying to use:

$(document).ready(function(){    
    $("#removeme").remove();
});

with the HTML

<div id="removeme">If JS enabled remove this!</div>

I've been searching for hours, using many variations including getElementById. No luck.

Javascript/jQuery aren't languages I use often.

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.

Share Improve this question edited Jul 4, 2013 at 20:27 Nick Moreau asked Jul 4, 2013 at 19:45 Nick MoreauNick Moreau 111 gold badge1 silver badge4 bronze badges 6
  • 5 You're looking for the <noscript> tag. – SLaks Commented Jul 4, 2013 at 19:48
  • 4 It works for me. jsfiddle/barmar/epsZe – Barmar Commented Jul 4, 2013 at 19:48
  • 2 Are you including jQuery? – Felix Kling Commented Jul 4, 2013 at 19:49
  • Are you getting any errors in the Javascript console? – Barmar Commented Jul 4, 2013 at 19:49
  • Works fine: jsfiddle/SCxxe – Learner Commented Jul 4, 2013 at 19:57
 |  Show 1 more ment

3 Answers 3

Reset to default 2
$('#remove').html('');

or

$('#remove').empty();

That will remove that text if JS is enabled.

The code you are showing in your question works, as proven by @barmar :

"http://jsfiddle/barmar/epsZe/"

If it doesn't work for you, it is most likely because you have an error in your Javascript before you reach the code displayed here. Also, ensure that you have included Jquery before-hand.

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 :

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.

try this $("#removeme").replaceWith("");

发布评论

评论列表(0)

  1. 暂无评论