Hide parent element with onclick function

前端 未结 3 1171
暗喜
暗喜 2020-12-19 15:10

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 cli

相关标签:
3条回答
  • 2020-12-19 15:23
    <div class="parent">
     <p id="dismiss" onclick="dismiss(this);">dismiss this box</p>
    </div>
    
    function dismiss(el){
      el.parentNode.style.display='none';
    };
    
    0 讨论(0)
  • 2020-12-19 15:29

    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 讨论(0)
  • 2020-12-19 15:43

    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.

    0 讨论(0)
提交回复
热议问题