Javascript 空不是一个函数
我很难找到删除列表中所有子项的代码中的错误:Javascript 空不是一个函数,javascript,jquery,drop-down-menu,Javascript,Jquery,Drop Down Menu,我很难找到删除列表中所有子项的代码中的错误: document.getElementById('btnGetExistingFiles')。onclick=function(){ var ul=document.getElementById('listExisting'); //我们将文件拆分成一个数组 var existingFiles=“a.yml b.yml c.yml d.yml”。拆分(“”); //如果列表中已经包含一些值,我们将删除它们 if(ul.hasChildNodes()
document.getElementById('btnGetExistingFiles')。onclick=function(){
var ul=document.getElementById('listExisting');
//我们将文件拆分成一个数组
var existingFiles=“a.yml b.yml c.yml d.yml”。拆分(“”);
//如果列表中已经包含一些值,我们将删除它们
if(ul.hasChildNodes()){
ul.empty();
}
//对于每个文件,我们在列表中添加一个元素
existingFiles.forEach(函数(文件名){
log(“添加文件”);
var li=document.createElement(“li”);
var a=document.createElement(“a”);
a、 setAttribute(“href”、“#”);
a、 setAttribute(“类”、“现有文件”);
a、 appendChild(document.createTextNode(文件名))
李.儿童(a);
ul.儿童(li);
});
}
下拉示例
没有Element.prototype.empty属性。
但是jquery中有一个:
$(ul).empty();
While是一个jquery函数。。我认为您需要使用ul.innerHTML=''代码>而不是ul.empty()
document.getElementById('btnGetExistingFiles')。onclick=function(){
var ul=document.getElementById('listExisting');
//我们将文件拆分成一个数组
var existingFiles=“a.yml b.yml c.yml d.yml”。拆分(“”);
//如果列表中已经包含一些值,我们将删除它们
if(ul.hasChildNodes()){
ul.innerHTML='';
}
//对于每个文件,我们在列表中添加一个元素
existingFiles.forEach(函数(文件名){
log(“添加文件”);
var li=document.createElement(“li”);
var a=document.createElement(“a”);
a、 setAttribute(“href”、“#”);
a、 setAttribute(“类”、“现有文件”);
a、 appendChild(document.createTextNode(文件名))
李.儿童(a);
ul.儿童(li);
});
}
下拉示例
Javascript没有empty()
方法。您可能会对jQuery感到困惑。如果要使用jQuery方法,则需要将ul
转换为jQuery对象:$(ul).empty()
为什么嵌入jquery而不使用它?在JavaScript
中,应该使用ul.innerHTML=''代码>。哦,是的,我没有用JQuery获取元素,我会修复它的。谢谢你的帮助answer@Jerome不客气。。祝你今天愉快:-)