admin管理员组文章数量:1335596
I have a list like this:
<li class="nav" id="1"><a href="#detail">a</a></li>
<li class="nav" id="2"><a href="#detail">b</a></li>
<li class="nav" id="3"><a href="#detail">c</a></li>
Now I want to use jQuery to save the id (1,2 or 3) which was clicked. How to do this?
I have a list like this:
<li class="nav" id="1"><a href="#detail">a</a></li>
<li class="nav" id="2"><a href="#detail">b</a></li>
<li class="nav" id="3"><a href="#detail">c</a></li>
Now I want to use jQuery to save the id (1,2 or 3) which was clicked. How to do this?
Share Improve this question edited Feb 1, 2012 at 15:04 fivedigit 18.7k6 gold badges58 silver badges61 bronze badges asked Jan 31, 2012 at 16:43 gurehbguigurehbgui 14.7k33 gold badges111 silver badges190 bronze badges 2- omitting for a while that those ID are not valid, where do you want to "save" the id? have you an event handler already defined somewhere? have you already made a search? is this a duplicate of stackoverflow./questions/451785/… – Fabrizio Calderan Commented Jan 31, 2012 at 16:47
- correct link : stackoverflow./questions/3545341/… – Fabrizio Calderan Commented Jan 31, 2012 at 16:53
3 Answers
Reset to default 7Try the following (jQuery 1.7 and above)
$('li.nav a').on('click', function (e) {
e.preventDefault();
var id = $(this).parent().attr('id');
...
});
Fiddle: http://jsfiddle/VKpRY/
jQuery 1.6 and earlier
$('li.nav a').click(function (e) {
e.preventDefault();
var id = $(this).parent().attr('id');
...
});
$("a").on('click',function (ee) {
ee.preventDefault();
alert($(this).parents("li:first").attr("id"));
})
For each selected jQuery object, simply use .attr('id');
. Example follows:
$('li.nav').each(function () {
var id = $(this).attr('id');
});
Hope this helps,
Pete
本文标签: javascriptHow to get the ID of a selected ltligt or ltagt with jQueryStack Overflow
版权声明:本文标题:javascript - How to get the ID of a selected <li> or <a> with jQuery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742394045a2466560.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论