jQuery从if语句获取“ this”

我正在尝试使用jquery从元素中获取唯一值,当我滚动到屏幕上的某个位置时,该值应该会出现。当元素在屏幕上时,postId 15应该到达jQuery代码。


这是我的代码:


$(document).scroll(function() {

    if($("p.viewedPost").is(':onScreen')) {

        var postId = $(this).attr("postId");

        console.log("Element appeared on Screen " + postId);

    }

    else {

        console.log("Element not on Screen");

        //do all your stuffs here when element is not visible.

    }

});

问题是我有多个postId,所以我不能使用 $("p.viewedPost").attr("postId");


它必须是 $(this).attr("postId");


但是,当我使用“ this”时,postId似乎是未定义的。那么我怎样才能$("p.viewedPost").is(':onScreen')拥有一个this?


GCT1015
浏览 251回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript