隐藏Div的父母 [英] Hide Parent of Div

查看:106
本文介绍了隐藏Div的父母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想隐藏父div comments_section

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

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

我试过这个:

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

哪个有效,但在IE 9中给我这个错误:

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


无法获取属性'parentNode'的值:object是null或未定义。

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

我是JavaScript的新手,所以我不确定如何修复此错误或是否是即使是正确的方法。提前致谢。

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.

推荐答案

好吧,你标记了这个jQuery所以我将提供jQuery方式:

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

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

编辑:@bobek首先说明问题发生的原因。他是对的。我应该注意到,使用jQuery,你仍然有问题,但没有错误。

@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.

这篇关于隐藏Div的父母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆