jquery selector: get both html content of the li object and the href value
I have the below sample code for a navigation list:
<div id="menu">
<ul>
<li><a href="#sect1">link 1</a></li>
<li><a href="#sect2">link 2</a></li>
<li><a href="#sect3">link 3</a></li>
</ul>
</div>
and some jquery code:
$("#menu li").click(function () {
var mylicontent=$(this).开发者_如何学JAVAhtml();
});
I want to get both html content of the li object and the href value.
You can do like:
$("#menu li").click(function () {
var mylicontent = $(this).html();
var mylik = $(this).find('a').attr('href');
});
Or you can also do like:
$("#menu li").click(function () {
var mylicontent = $(this).html();
var mylik = $('a', this).attr('href');
});
It's best if you attach click on the a
and not on the li
.
Then you'd do something like $(this).parent().html()
精彩评论