简体   繁体   中英

jquery - Finding the href attribute of a jquery multiple selector

so I was wondering, assuming you have a multiple selector as below:

$('.BOO li', '.FOO li a').click(function() {

});

and you want to find the href value of both classes, using the this keyword, how do you achieve that?

Because if you go ahead and do it like below,

$('.BOO li', '.FOO li a').click(function() {
    $(this).attr("href");
   //do stuff
    });

.BOO li is obviously left out.

Is there a way to target the this of only the .BOO1 li and find it's a ?

Additional info:- of course, an alternative would've been selecting the .BOO1 li a directly

You mean like this?

$('.BOO li', '.FOO li a').click(function() {
    var href;
    if($(this).attr("href"))
        href = $(this).attr("href");
    else
        href = $(this).find("a").attr("href");
});

Here is a more concise, but more confusing way to do the above:

$('.BOO li', '.FOO li a').click(function() {
    var href = $(this).attr("href") ? $(this).attr("href") : $(this).find("a").attr("href");
});

使用.each遍历每个选定的元素,并在回调函数中使用$(this).attr("href")获得您的值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM