1
我想要获得附加列表元素的ID。在尝试了几个小时并从jquery get the id/value of LI after click function和How to get the id of html list item using jquery?中读取多个源文件后,我仍然无法弄清楚我的代码中的错误。查找附加(通过函数)html列表元素与jQuery的ID
上面的来源和我想要实现的唯一区别是他们已经有列表元素存在于html里面,因为我通过jquery函数追加列表。
这是我的代码。
的Html
<button id="display">Display</button>
<div class="main">
<ul>
</ul>
</div>
<p id="show"> </p>
jQuery的2.1.0
var i = 1;
$('#display').click(function() {
$(".main ul").append(
$('<li>').attr("id",i).append(
$('<p>').append("Stackoverflow")
));
i++;
});
$('.main ul li').click(function() {
$('#show').text("List ID =" + this.id);
});
谢谢您的时间。
使用'$( “主UL ”)。在(“ 点击”, “li”,function()...);' – Barmar 2015-02-12 02:26:09
请参阅http://jsfiddle.net/mtg39cd5/1/ – Barmar 2015-02-12 02:26:58
感谢您的快速回复Barmar现在正在工作,我将检查on()的文档,但是您能否很快解释为什么click没有成功,但是.on做到了。 – 2015-02-12 02:36:32