javascript - Hide Parent of Div - Stack Overflow

I'm simply trying to hide the parent div ments_section.<div class="content content_green&q

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

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

I tried this:

document.getElementById("ments_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.

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

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

I tried this:

document.getElementById("ments_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.

Share Improve this question edited Jul 30, 2012 at 22:53 user1373748 asked Jul 30, 2012 at 22:40 user1373748user1373748 331 silver badge6 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

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

jQuery('#ments_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.

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="ments_section"></div>
</div>

Use jQuery for this:

$("#ments_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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745190244a4615831.html

相关推荐

  • javascript - Hide Parent of Div - Stack Overflow

    I'm simply trying to hide the parent div ments_section.<div class="content content_green&q

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信