javascript appendChild不起作用 [英] javascript appendChild doesn't work

查看:424
本文介绍了javascript appendChild不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这个功能在firefox 3.6上发生错误

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];
        }           
    }

DOM中的哪些广告像
< a xmlns =http://www.w3.org/1999/xhtml>

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];
        }           
    }

但是我收到以下错误appendChild:

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

编辑:
示例:

Example:

split [0]包含: / p>

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>"

我想更新8个新内容的锚点,包含在[0]中,分割[1] ...拆分[7 ]

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

推荐答案

拆分[i] 是问题。 appendChild 将DOM元素附加到现有的DOM元素,但是您似乎试图附加一个字符串值。如果要使用 appendChild ,请创建一个容器元素,然后使用 innerHTML 来插入字符串,或者只是使用 innerHTML 。这是一个错误,你不能附加一个字符串作为DOM元素,我会说。另请参阅appendChild上的 MDN页面

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.

这篇关于javascript appendChild不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆