javascript - innerHtml not working for nested div tags - Stack Overflow

I am not able to replace .innerHtml for a div tag which is nested in other div tags. I used firebug to

I am not able to replace .innerHtml for a div tag which is nested in other div tags. I used firebug to make sure that getElementById() is working.

<div class="a">
<div class="b"><a href="myUrl"><div id="hello"></div></a></div>
</div>

In javascript code I am using:

document.getElementById("hello").innerHTML = "hello world";

Using firebug, I have verified that "hello" div is picked up (that is, it is not null). But the string "hello world" is not showing up. Kindly help. Thanks.

I am not able to replace .innerHtml for a div tag which is nested in other div tags. I used firebug to make sure that getElementById() is working.

<div class="a">
<div class="b"><a href="myUrl"><div id="hello"></div></a></div>
</div>

In javascript code I am using:

document.getElementById("hello").innerHTML = "hello world";

Using firebug, I have verified that "hello" div is picked up (that is, it is not null). But the string "hello world" is not showing up. Kindly help. Thanks.

Share Improve this question asked Oct 10, 2011 at 16:51 SunSun 5802 gold badges9 silver badges31 bronze badges 7
  • Do NOT put a div container inside an a element. Your DOM should be like this: <div><a>Link</a></div>. It isn't related to the problem you're having, but you will be flamed for it. – Some Guy Commented Oct 10, 2011 at 16:54
  • I am using innerHTML. That was a typo in question. – Sun Commented Oct 10, 2011 at 16:55
  • Here is a jsfiddle showing that your code already works. I'd start looking for another "hello" id somewhere on the page. – Pointy Commented Oct 10, 2011 at 16:56
  • If you're saying it was just a typo in the question, and it still doesn't work, there's something stopping your JavaScript from executing. An error, perhaps. This works flawlessly (here). – Some Guy Commented Oct 10, 2011 at 16:58
  • @Amaan in HTML5 that is no longer true. The content model for <a> tags in HTML5 is transparent, which means essentially that it inherits its content model from a non-transparent parent element – Pointy Commented Oct 10, 2011 at 16:59
 |  Show 2 more ments

2 Answers 2

Reset to default 5

use innerHTML rather than innerHtml. note the capitalization.

On a side note, IE also supports innerText.

The example you provided works for me... so, I think you should make sure that the script is executed after the element has been created in the DOM.

For example, place the script element after the div you want to add contents to. So if this fixes the issue, then you should add an event listener to the window’s onload event which will run all scripts.

And as Amaan said, don’t place a div element inside an a element.

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

相关推荐

  • javascript - innerHtml not working for nested div tags - Stack Overflow

    I am not able to replace .innerHtml for a div tag which is nested in other div tags. I used firebug to

    9天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信