您能告诉我如何获取 li
的点击事件吗?我使用 jstree
动态制作 li
元素。
我按下 add
按钮,它会在 jstree
内部创建一个 li
元素。我们可以获取该元素的点击事件吗?
按add
两次。它生成li
元素。我想获取li
的点击事件。获取其id的警报。?
http://jsfiddle.net/GS4u3/4/
$('#uu').click(function () {
var ref = $('#tree').jstree(true),
sel = ref.get_selected();
if (!sel.length) {
alert('thank')
sel = ref.create_node("#", {"id" : node_count+1, "text" : node_count+1});
node_count++;
} else
{
sel = sel[0];
sel = ref.create_node(sel, {"id" : node_count+1, "text" : node_count+1});
node_count++;
}
/*if (sel) {
ref.edit(sel);
}*/
ref.deselect_all();
});
最佳答案
这样就可以完成任务了。由于 li
是动态创建的,因此您需要delegate ,已被 on 取代.
$("#tree").on("click", "li > a", function() {
var id = $(this).closest("li").attr("id");
$(this).siblings(".jstree-icon").click();
alert(id);
});
编辑:仅使文本可点击,而不是箭头。
编辑2:http://jsfiddle.net/GS4u3/8/ (编辑 4,新的 jsfiddle 并更新了您的更改)
EDIT3:编辑代码,单击文本即可伸展树(Splay Tree)。
关于javascript - jquery中如何获取li元素的点击事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23456808/