使用javascript显示[object HTMLUListElement]的内容 [英] Display content of [object HTMLUListElement] using javascript
问题描述
我有一个javascript函数,它根据传入的数组生成一个ul列表,使用类似的方法 - 创建一个< ul>并根据传递的数组填充它
然而,当我执行以下操作时......
document.getElementById(list)。innerHTML = generateListFromArray(array);
所有打印的都是
[object HTMLUListElement]
任何人都可以告诉我如何打印内容到HTML格式?
您正在创建一个合适的UL元素( HTMLUListElement
),这很棒。您可以直接使用 ,只需将它附加到您的目标即可:
document.getElementById(list ).appendChild(generateListFromArray(阵列));
如果目标已经包含要替换的内容 / em>(而不是添加到),您可以先清除目标元素:
var list = document.getElementById(清单); //获取目标元素
list.innerHTML =; //删除以前的内容
list.appendChild(generateListFromArray(array)); //追加生成的UL
完全没有理由将您创建的元素转换为首先使用 如果 I have a javascript function which generates a ul list based on an array being passed in using a similar approach to this - Create a <ul> and fill it based on a passed array However, when I do the following... All that gets printed is Can anyone tell me how to print the contents into the div as HTML? You're creating a proper UL element ( If the target already contains content you want to replace (rather than add to), you can clear the target element first: There's simply no reason, at all, to convert the element you created to markup first (by using If
这篇关于使用javascript显示[object HTMLUListElement]的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! .innerHTML
或 .outerHTML
对的返回值进行标记(generateListFromArray <
list
也是 ul
并且您想替换,您可以使用 insertBefore
和 removeChild
:
var list = document.getElementById(list); //获取目标元素
var parent = list.parentNode; //获取它的父
var newList = generateListFromArray(array); //获取新的
parent.insertBefore(
newList,//插入新的列表...
list // ...在旧的之前
);
parent.removeChild(list); //删除旧的
newList.id =list; //给新的列表ID
//旧的
document.getElementById("list").innerHTML = generateListFromArray(array);
[object HTMLUListElement]
HTMLUListElement
), which is great. You can use that directly by simply appending it to your target:document.getElementById("list").appendChild(generateListFromArray(array));
var list = document.getElementById("list"); // Get the target element
list.innerHTML = ""; // Remove previous content
list.appendChild(generateListFromArray(array)); // Append your generated UL
.innerHTML
or .outerHTML
on the return value of generateListFromArray
).list
is also a ul
and you want to replace it, you can do that with insertBefore
and removeChild
:var list = document.getElementById("list"); // Get the target element
var parent = list.parentNode; // Get its parent
var newList = generateListFromArray(array); // Get the new one
parent.insertBefore(
newList, // Insert the new list...
list // ...before the old one
);
parent.removeChild(list); // Remove the old
newList.id = "list"; // Give the new list the ID the
// old one had