Hide Parent of Div

冷暖自知 提交于 2019-12-10 19:44:09

问题


I'm simply trying to hide the parent div comments_section.

<div class="content content_green">
    <div id="comments_section"></div>
</div>

I tried this:

document.getElementById("comments_section").parentNode.style.display = "none"

which worked, but gives me this error in IE 9:

Unable to get value of the property 'parentNode': object is null or underfined.

I'm very new to JavaScript, so I'm not sure how to fix this error or if it's even the right approach. Thanks in advance.


回答1:


Well, you tagged this jQuery so I'll provide the "jQuery way":

jQuery('#comments_section').parent().hide();

EDIT: @bobek gives the reason why your problem occurred in the first place. He is correct. I should note that with jQuery, you'd still have the problem but no error.




回答2:


You didn't close the child div. IE is rather prone to errors and will yield at you.

<div class="content content_green">
       <div id="comments_section"></div>
</div>



回答3:


Use jQuery for this:

$("#comments_section").parent().hide()

Or, you could do this the right way:

$("div.content.content_green").hide()

Currently you don't use jquery. Visit jQuery documentation



来源:https://stackoverflow.com/questions/11730765/hide-parent-of-div

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!