JavaScript创建项目符号和子项目符号项
JavaScript创建项目符号和子项目符号项,javascript,jquery,html,css,Javascript,Jquery,Html,Css,函数f(){ $(“#main”).focus(); } $(文档).ready(函数(e){ $(“#main”).focus(); $(“按钮”).hide(); $(“li”).live(“dblclick”,function(){ var节点=document.createElement(“ul”); var node_li=document.createElement(“li”);//创建一个节点 var textnode=document.createTextNode(“”;//创建
函数f(){
$(“#main”).focus();
}
$(文档).ready(函数(e){
$(“#main”).focus();
$(“按钮”).hide();
$(“li”).live(“dblclick”,function(){
var节点=document.createElement(“ul”);
var node_li=document.createElement(“li”);//创建一个节点
var textnode=document.createTextNode(“”;//创建一个文本节点
节点\ li.appendChild(textnode);
node.appendChild(node_li);
//将文本附加到
//document.getElementById(“main”).appendChild(节点);
$(this).append(节点);
//警报($(this.parent().children().index(this));
})
$('li').live('keypress',函数(e){
var p=e,其中;
如果(p==9){
var节点=document.createElement(“ul”);
var node_li=document.createElement(“li”);//创建一个 节点
var textnode=document.createTextNode(“”;//创建一个文本节点
节点\ li.appendChild(textnode);
node.appendChild(node_li);
//将文本附加到
//document.getElementById(“main”).appendChild(节点);
$(this).append(节点);
}
});
});
//$(“.fb li”).live
函数li_new(){
}
按“输入下一个lile”双击子行的行
jQuerylive()
已被弃用,您应该改用on()
。因为要动态创建列表元素,所以双击时必须使用委托事件处理程序
用tab键附加子节点有点棘手。您必须手动找到光标的位置。我用了一个由蒂姆提出的解决方案来解决另一个问题
我还通过创建附加子节点的函数使代码更加模块化
您还拥有大量无用的和注释过的代码。在发布问题之前,一定要把这些问题处理掉。这使我们的工作容易多了
函数焦点(){
$(“#main”).focus();
}
//返回光标的位置
函数getCursorPosition(){
if(window.getSelection&&window.getSelection().getRangeAt){
返回窗口.getSelection().getRangeAt(0);
}else if(document.selection&&document.selection.createRange){
return document.selection.createRange();
}
}
//将新的子节点附加到给定元素
函数附加子节点($el){
var节点=document.createElement(“ul”);
var node_li=document.createElement(“li”);//创建一个节点
var textnode=document.createTextNode(“”;//创建一个文本节点
节点\ li.appendChild(textnode);
node.appendChild(node_li);
$($el).append(节点);
}
$(文档).ready(函数(e){
$(“#main”).focus();
$(“按钮”).hide();
//委托的事件处理程序
$(文档).on('dblclick','li',函数(){
追加子节点($(this));
});
$('.currentlist')。on('keydown',函数(e){
var p=e,其中;
如果(p==9){
e、 预防默认值();
//找出光标所在的位置
var cursorposis=getCursorPosition();
appendSubNode(cursorPos.commonAncestorContainer);
}
});
});代码>
按“输入下一个lile”双击子行的行
首先,不要使用.live
。使用下一步,把你的代码放在一把小提琴上。Tab键现在仍然不能与你的解决方案一起工作。请参阅代码段,当双击列表li项时,它会生成一个新的子li,但当我单击Tab键时,它仍然是空的。我想用TAB键创建子列表。是的,它不起作用。我编辑了答案。抱歉花了这么长时间,还有其他事情要做,现在好点了吗?