简体   繁体   中英

jQuery find - can I use a callback?

So I'm trying to figure out if I can call a function inside of find() as below but I'm not getting anything returned to the console. Is this possible with find() or do I need to find an alternative?

$(".tdInner1").find(".block", function () {
    if( $(this).next().hasClass("continuation") ) {
        console.log("yes");
    } else {
        console.log("no");
    }
});

Sounds like you want .each() .

$(".tdInner1").find(".block").each(function () {
    if( $(this).next().hasClass("continuation") ) {
        console.log("yes");
    } else {
        console.log("no");
    }
});

Or maybe .filter()

$(".tdInner1").find(".block").filter(function () {
    return $(this).next().hasClass("continuation");
});

You need jQuery each() .

$(".tdInner1").find(".block").each( function () {
    if( $(this).next().hasClass("continuation") ) {
        console.log("yes");
    } else {
        console.log("no");
    }
});

You can read more about jQuery each() in Official Documentation

or you can use filter()

var block = $(".tdInner1 .block");

var continuation_is_next = block.next().filter(".continuation").prev();

or like this

var continuation_is_next=  $(".tdInner1 .block + .continuation").prev();

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