使用 jQuery 获取列表项的值 [英] Get value of List Item with jQuery

查看:28
本文介绍了使用 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 获取列表项的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆