Get value of List Item with jQuery(使用 jQuery 获取列表项的值)
问题描述
如何用jQuery获取列表项onClick
事件的值和索引?
例如:
How to get value and index of list item onClick
event with jQuery?
For example:
<ul id='uItem'>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
</ul>
推荐答案
结合使用.index()
和 .text()
(或 .html()
,如果你愿意):
Combine the use of .index()
and .text()
(or .html()
, if you wish):
$('#uItem li').click(function() {
var index = $(this).index();
var text = $(this).text();
alert('Index is: ' + index + ' and text is ' + text);
});
这篇关于使用 jQuery 获取列表项的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!