使用javascript显示[object HTMLUListElement]的内容 [英] Display content of [object HTMLUListElement] using javascript

查看:3082
本文介绍了使用javascript显示[object HTMLUListElement]的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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

完全没有理由将您创建的元素转换为首先使用 .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
//旧的


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

document.getElementById("list").innerHTML = generateListFromArray(array);

All that gets printed is

[object HTMLUListElement]

Can anyone tell me how to print the contents into the div as HTML?

解决方案

You're creating a proper UL element (HTMLUListElement), which is great. You can use that directly by simply appending it to your target:

document.getElementById("list").appendChild(generateListFromArray(array));

If the target already contains content you want to replace (rather than add to), you can clear the target element first:

var list = document.getElementById("list");     // Get the target element
list.innerHTML = "";                            // Remove previous content
list.appendChild(generateListFromArray(array)); // Append your generated UL

There's simply no reason, at all, to convert the element you created to markup first (by using .innerHTML or .outerHTML on the return value of generateListFromArray).

If 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

这篇关于使用javascript显示[object HTMLUListElement]的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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