开发者

Getting 'href' value of an anchor inside another element

开发者 https://www.devze.com 2022-12-17 22:56 出处:网络
Suppose I have this: <ul id=\"menu\"> <li><a href=\"someplace\">some text</a><li>

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

精彩评论

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