开发者

jquery selector: get both html content of the li object and the href value

开发者 https://www.devze.com 2023-01-01 05:59 出处:网络
I have the below sample code for a navigation list: <div id=\"menu\"> <ul> <li><a href=\"#sect1\">link 1</a></li>

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()

0

精彩评论

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