How can I grab an element inside of an <li> in jQuery?
I have the following <li>
- there are many of these, but this is just one example:
<li class="ui-li ui-li-static ui-body-d ui-li-has-icon io-sidebar-link io-sidebar-link-standard" data-theme="d" io-sidebar-section="0aa210f2-e811-4bae-aac0-649bf87fb240" io-object-view="/cms?url=ui/object&object=db13a9ad-2494-34bb-8a59-cb99fd308051" io-record-view="/cms?url=ui/record&object=db13a9ad-2494-34bb-8a59-cb99fd308051" style="display: block">
<img src="/document/423fc17f-08b2-46bb-a1db-a5395cd63b83/latest" class="ui-li-icon ui-li-thumb" height="16" width="16">
<div class="io-sidebar-link-text">Tasks</div>
开发者_StackOverflow中文版 </li>
I have a click event bound, and would like to print out the value of io-sidebar-link-text
. How can I do this?
$(this).('.io-sidebar-link-text')
is incorrect I believe.
Pretty close! Use $(this).find('.io-sidebar-link-text').text()
inside the click handler.
$(this).find(xyz)
is syntactic sugar for $(xyz, this)
; so checkout both the docs for the former, and the latter.
this will select only the li how has the attribute io-sidebar-link-text , and get this attribute's value with the attr() method
var title = $("li[io-sidebar-link-text]").attr("io-sidebar-link-text");
If it is always in a div:
$(this).find('div').text();
or if you want to select on the class:
$(this).find('.io-sidebar-link-text').text();
精彩评论