开发者

Get id attribute from another element in same list item with Jquery

开发者 https://www.devze.com 2023-02-24 07:00 出处:网络
<li class=\"ui-li ui-li-static ui-body-c\"> <p class=\"ui-li-aside ui-li-desc\"></p>
<li class="ui-li ui-li-static ui-body-c">
    <p class="ui-li-aside ui-li-desc"></p>
    <span id="122"></span>
    <h3 class="ui-li-heading"></h3>
    <p class="ui-li-desc"></p>
    <p class="ui-li-desc"><a class="ui-link">Report</a></p>
</li>

So here is my 开发者_JAVA百科HTML markup.

Goal: I need JQuery code that will give me the id value of the <span> element (so in this case: 122) within the same <li>, when I click the <a> in that same <li>.

How would I do this?

Thanks!


Assuming you only have one span tag in your list items, you should be able to get your span's id with this:

$('a.ui-link').click(function(){
   var id = $(this).closest('li').find('span:first').attr('id');
});


If you have only one span as in your html...

$('a.ui-link').live('click', function(){
   var id = $('span', $(this).closest('li')).attr('id');
});
0

精彩评论

暂无评论...
验证码 换一张
取 消