Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
834 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 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...