[英]javascript appendChild doesn't work
i'm having a bug on firefox 3.6 using this function我在使用这个 function 的 firefox 3.6 上有一个错误
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"> DOM 锚点中的哪些广告类似于“<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:但我在 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:拆分 [0] 包含:
"<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]我想用新内容更新 8 个锚点,包含在 splitted[0]、splitted[1]...splitted[7] 中
splitted[i]
is the problem. splitted[i]
是问题所在。 appendChild
appends a DOM-element to an existing DOM-element, but it looks like you ar trying to append a string value. appendChild
将 DOM 元素附加到现有的 DOM 元素,但看起来您正在尝试 append 字符串值。 If you want to use appendChild
, either create a container element and use innerHTML
for that to insert the string, or just use innerHTML
.如果您想使用
appendChild
,请创建一个容器元素并使用innerHTML
插入字符串,或者只使用innerHTML
。 It is not a bug that you can't append a string as DOM-element, I'd say.我想说,你不能 append 将字符串作为 DOM 元素并不是一个错误。 See also the MDN-page on appendChild.
另请参阅 appendChild 上的MDN 页面。
response.value.split("|");
Indicates to me that you are passing response
as a string.向我表明您将
response
作为字符串传递。 appendChild only works with elements. appendChild 仅适用于元素。 You can't append a child to a flat string.
你不能 append 一个孩子到一个扁平的字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.