简体   繁体   English

如何获取jquery中li元素的点击事件?

[英]how to get click event of li element in jquery?

can you please tell me how to get click event of li .I making li element dynamically using jstree . 你能告诉我如何获取li点击事件吗?我使用jstree动态地制作了li元素。 I am press add button it create a li element internally in jstree .can we get click event of that ? 我按下add按钮,它在jstree内部创建了一个li元素。我们可以得到它的click事件吗? Press add two time.it generate li element.I want to get click event of li .get alert of its id.? add两次time.it生成li元素。我想获取li点击事件。获取其ID的提醒。 http://jsfiddle.net/GS4u3/4/ 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();

}); });

This would do the job. 这样就可以了。 Since the li is created dynamically, you need to delegate , which has been superseded by on . 由于li是动态创建的,因此需要委托 ,该代理已被on取代。

$("#tree").on("click", "li > a", function() {
    var id = $(this).closest("li").attr("id");
    $(this).siblings(".jstree-icon").click();
    alert(id);
});

EDIT: Making only the text clickable and not the arrow. 编辑:仅使文本可单击而不是箭头。

EDIT2: http://jsfiddle.net/GS4u3/8/ (edit 4, new jsfiddle with your change updated) EDIT2: http : //jsfiddle.net/GS4u3/8/ (编辑4,更新了您的更改的新jsfiddle)

EDIT3: Edited code, to expand the tree upon clicking the text. EDIT3:编辑代码,以在单击文本时展开树。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM