jquery获得单击函数后LI的id /值 [英] jquery get the id/value of LI after click function
本文介绍了jquery获得单击函数后LI的id /值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
< ul id ='myid'>
< li id ='1'>第一< / li>
< li id ='2'>第二个< / li>
< li id ='3'>第三< / li>
< li id ='4'>第四< / li>
< li id ='5'>第五< / li>
< / ul>
如何警告li项目的id被点击(任何可以取代id的值都可能是值或其他的东西也可以工作)
请帮助
在此先感谢
Dave
解决方案
$ b点击(功能(){
this.id ='newId';
/ $ http://api.jquery.com/attr/
$(#myid li)。click(function(){
alert(this.id); // id clicked li通过直接访问DOMElement属性
alert($(this).attr('id')); // jQuery的.attr()方法,相同但更详细
alert($(this).html( )); //获取点击li
的innerHTML alert($(this).text()); //获取点击li的文本内容
});
如果您正在谈论用某些东西替换ID:
$ b点击(功能(){
this.id ='newId';
/ $
$(#myid li /更长的方法使用.attr()
$(this).attr('id','newId');
});
在这里演示。公平地说,你应该首先尝试阅读文档:
<ul id='myid'>
<li id='1'>First</li>
<li id='2'>Second</li>
<li id='3'>Third</li>
<li id='4'>Fourth</li>
<li id='5'>Fifth</li>
</ul>
How can I alert the id of the li item clicked(anything that can replace the id may be value or something else will also work)
please help
thanks in advance
Dave
解决方案
$("#myid li").click(function() {
alert(this.id); // id of clicked li by directly accessing DOMElement property
alert($(this).attr('id')); // jQuery's .attr() method, same but more verbose
alert($(this).html()); // gets innerHTML of clicked li
alert($(this).text()); // gets text contents of clicked li
});
If you are talking about replacing the ID with something:
$("#myid li").click(function() {
this.id = 'newId';
// longer method using .attr()
$(this).attr('id', 'newId');
});
Demo here. And to be fair, you should have first tried reading the documentation:
- http://api.jquery.com/category/selectors/
- http://api.jquery.com/category/events/
- http://api.jquery.com/attr/
这篇关于jquery获得单击函数后LI的id /值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文