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

javascript - Hide parent element with onclick function - Stack Overflow

programmeradmin2浏览0评论

I use jQuery most of the time, so I am having a bit of trouble with the following (simple) javascript:

I want to dismiss(hide) the parent element of a p tag when clicking on it:

HTML:

<div class="parent">
     <p id="dismiss" onclick="dismiss();">dismiss this box</p>
</div>

JS:

function dismiss(){
    document.getElementById('dismiss').pDoc.parentNode.style.display='none';
};

Fiddle: /

But this is not working. What would be the correct code?

Thanks

I use jQuery most of the time, so I am having a bit of trouble with the following (simple) javascript:

I want to dismiss(hide) the parent element of a p tag when clicking on it:

HTML:

<div class="parent">
     <p id="dismiss" onclick="dismiss();">dismiss this box</p>
</div>

JS:

function dismiss(){
    document.getElementById('dismiss').pDoc.parentNode.style.display='none';
};

Fiddle: http://jsfiddle.net/CUqmn/3/

But this is not working. What would be the correct code?

Thanks

Share Improve this question asked Jul 1, 2013 at 8:08 DextrousDaveDextrousDave 6,79337 gold badges95 silver badges136 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 15

http://jsfiddle.net/CUqmn/4/

function dismiss(){
      document.getElementById('dismiss').parentNode.style.display='none';
};

BTW, as jsfiddle wrap javascript code in loader function, use no wrap in left select box to get it work on jsfiddle.

<div class="parent">
 <p id="dismiss" onclick="dismiss(this);">dismiss this box</p>
</div>

function dismiss(el){
  el.parentNode.style.display='none';
};

You could try:

HTML:

<div class="parent">
     <p id="dismiss" onclick="dismiss(this.parentNode);">dismiss this box</p>
</div>

JS:

function dismiss(delete){
    delete.style.display='none';
};

This will delete the parent element. Also I just recently found out that you can hide the parent of a parent element like this:

HTML:

<div class="parent">
     <p id="dismiss" onclick="dismiss(this.parentNode);">dismiss this box</p>
</div>

JS:

function dismiss(delete){
    delete.parentNode.style.display='none';
};

Not relevant to this but if you ever want to try it it's there.

Sorry for my really late reply. 2 years later lol.

发布评论

评论列表(0)

  1. 暂无评论