运行以下代码,我得到的结果如下:
[object HTMLHeadingElement]
[object HTMLHeadingElement]
[object HTMLHeadingElement]
[object HTMLHeadingElement]
但它应该有一些ul
列表的结果。我不知道我哪里出错了。我错过了什么?
function textContent(ele,value){
var content = ele.textContent;//check if textContent is defined
if(value === undefined){//no value passed, so return current text
if(content !== undefined) return content;
else return ele.innerText;
}else{//a value is passed, so set text
if(content !== undefined) ele.textContent = value;
else ele.innerText = value;
}
}
(function(){
var titles = document.querySelectorAll('.docHomeDetails h3');
var sideList = document.getElementsByClassName('sideList')[0];
var ul = document.querySelectorAll('.sideList ul')[0];
window.onload = function(){
for(var i = 0; i< titles.length; i++){
var li = document.createElement('li');
textContent(li, titles[i]);//setting plain text into li element
ul.appendChild(li);
}
}
}());
答案 0 :(得分:0)
错误在于:
textContent(li, titles[i]);//setting plain text into li element
您尝试分配元素本身而不是文本内部。应该是:
textContent(li, titles[i].innerHTML);
我的fiddle。