admin管理员组文章数量:1399933
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
2 Answers
Reset to default 5use 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.
本文标签: javascriptinnerHtml not working for nested div tagsStack Overflow
版权声明:本文标题:javascript - innerHtml not working for nested div tags - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744141439a2592638.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论