最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery find - can I use a callback? - Stack Overflow

programmeradmin4浏览0评论

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");
    }
});

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");
    }
});
Share Improve this question asked Aug 15, 2012 at 18:34 AaronAaron 5502 gold badges6 silver badges23 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 11

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();
发布评论

评论列表(0)

  1. 暂无评论