4

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

4

3 回答 3

15

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

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

顺便说一句,作为 jsfiddle 在加载器函数中包装 javascript 代码,在左选择框中使用不换行以使其在 jsfiddle 上工作。

于 2013-07-01T08:10:11.687 回答
7
<div class="parent">
 <p id="dismiss" onclick="dismiss(this);">dismiss this box</p>
</div>

function dismiss(el){
  el.parentNode.style.display='none';
};
于 2013-07-01T08:12:06.603 回答
1

你可以试试:

HTML:

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

JS:

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

这将删除父元素。另外我最近发现你可以像这样隐藏父元素的父元素:

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';
};

与此无关,但如果您想尝试它,它就在那里。

对不起,我真的迟到了回复。2年后哈哈。

于 2015-06-03T21:57:51.057 回答