admin管理员组文章数量:1316827
I have code that suppose to find the first child (div) of a div and than change its content, here is my code:
var headDiv = document.getElementById('sliderContainer');
var childDiv = headDiv.childNodes[0];
childDiv.innerHTML = 'Working';
that seems to not working, what's the problem here?
live : ,html,live
the full code :
<!doctype html>
<html>
<head>
<script type="javascript">
function scrollit(){
var headDiv = document.getElementById('sliderContainer');
var childDiv = headDiv.childNodes[0];
childDiv.innerHTML = 'Working';
}
</script>
<style type="text/css">
#sliderContainer{width:100px;height:100px;overflow:hidden;}
.sliderContent{width:100px;height:100px;float:left;}
</style>
</head>
<body>
<div id="sliderContainer">
<div class="sliderContent" style="background-color:blue;">s</div>
<div class="sliderContent" style="background-color:yellow;">a</div>
</div><br/><br/>
<button onclick="scrollit();">scroll it</button>
</body>
</html>
I have code that suppose to find the first child (div) of a div and than change its content, here is my code:
var headDiv = document.getElementById('sliderContainer');
var childDiv = headDiv.childNodes[0];
childDiv.innerHTML = 'Working';
that seems to not working, what's the problem here?
live : http://jsbin./aqozef/edit#javascript,html,live
the full code :
<!doctype html>
<html>
<head>
<script type="javascript">
function scrollit(){
var headDiv = document.getElementById('sliderContainer');
var childDiv = headDiv.childNodes[0];
childDiv.innerHTML = 'Working';
}
</script>
<style type="text/css">
#sliderContainer{width:100px;height:100px;overflow:hidden;}
.sliderContent{width:100px;height:100px;float:left;}
</style>
</head>
<body>
<div id="sliderContainer">
<div class="sliderContent" style="background-color:blue;">s</div>
<div class="sliderContent" style="background-color:yellow;">a</div>
</div><br/><br/>
<button onclick="scrollit();">scroll it</button>
</body>
</html>
Share
Improve this question
edited Nov 6, 2011 at 17:59
Felix Kling
817k181 gold badges1.1k silver badges1.2k bronze badges
asked Nov 6, 2011 at 17:44
homerunhomerun
20.8k15 gold badges48 silver badges71 bronze badges
1
- please post the HTML content as well – Saket Commented Nov 6, 2011 at 17:46
1 Answer
Reset to default 7It is likely that the first child node is a text node and not an element node. This is the case, for example, when your HTML looks like this:
<div>
<div> I'm the first element child</div>
</div>
The line break after the first div
and the spaces before the second one are one text node.
To get the element nodes, use children
[MDN]:
headDiv.children[0].innerHTML = "Working";
Note: IE6 - IE8 include ment nodes in children
as well. As long as you have no ments there, it is fine.
Alternatively you can traverse the child nodes, looking for the first element node:
var child = element.firstChild;
while(child && child.nodeType !== 1) child = child.nextSibling;
Reference: Node.firstChild
, Node.nodeType
, Node.nextSibling
.
If you want to change the value of a text node, you have to change the nodeValue
[MDN] property:
node.nodeValue = "Working";
本文标签: javascriptChange text of first childStack Overflow
版权声明:本文标题:javascript - Change text of first child - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742014762a2413560.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论