[英]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).html();
});
I want to get both html content of the li object and the href value. 我想同时获取li对象的HTML内容和href值。
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
. 最好附加
a
而不是li
。
Then you'd do something like $(this).parent().html()
然后,您将执行类似
$(this).parent().html()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.