Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
780 views
in Technique[技术] by (71.8m points)

dom - javascript appendChild doesn't work

i'm having a bug on firefox 3.6 using this function

    function GetRefreshedResults(response)
    {   
        var splitted = response.value.split("|");
        var panel = document.getElementById('my-glider');
        var anchors = panel.getElementsByTagName('a');
        for (var i=0; i<anchors.length; i++)
        {
          anchors[i].innerHTML=splitted[i];
        }           
    }

which ads in DOM anchors like "< a xmlns="http://www.w3.org/1999/xhtml">

I'm now trying to use this instead:

     function GetRefreshedResults(response)
    {   
        var splitted = response.value.split("|");
        var panel = document.getElementById('my-glider');
        var anchors = panel.getElementsByTagName('a');
        for (var i=0; i<anchors.length; i++)
        {
            anchors[i].empty();
            anchors[i].appendChild(splitted[i]);
          //  anchors[i].innerHTML=splitted[i];
        }           
    }

but i get the following error in appendChild :

        Uncaught Error: NOT_FOUND_ERR: DOM Exception 8

i don't understand why it's not working. can anyone help me ? thanks

EDIT: Example:

splitted[0] contains :

   "<div class="var">Visits</div><div class="percent-zero">0%</div><div class="val">0<div class="val-alt">Unique Visits: 0</div></div>"

i want to update 8 anchors with new content, contained in splitted[0], splitted[1]...splitted[7]

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

splitted[i] is the problem. appendChild appends a DOM-element to an existing DOM-element, but it looks like you ar trying to append a string value. If you want to use appendChild, either create a container element and use innerHTML for that to insert the string, or just use innerHTML. It is not a bug that you can't append a string as DOM-element, I'd say. See also the MDN-page on appendChild.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...