最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Change text of first child - Stack Overflow

programmeradmin2浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 7

It 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";
发布评论

评论列表(0)

  1. 暂无评论