开发者

Getting 'href' value of an anchor inside another element

Suppose I have this:

<ul id="menu">
<li><a href="someplace">some text</a><li>
...
</ul>

I want to make every li clickable so I need to extract the value of href like this:

$('#menu li').click(function() {
    var link_to = $(SELECTOR THAT I NEED).attr('href');
    window.location = link_to;
});

what's the correct code for this? Is th开发者_C百科ere a better way?


$('#menu li').click(function() {
    var link_to = $(this).children("a").eq(0).attr('href');
    window.location = link_to;
});


Since you have a link in there, why not use that ?

you could set it to display:block with css if you want it to take up the whole li width ..

#menu li a{display:block;}

and avoid using jquery for built in functionality .. (if i understand correctly..)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜