为了账号安全,请及时绑定邮箱和手机立即绑定

jQuery从if语句获取“ this”

jQuery从if语句获取“ this”

GCT1015 2021-03-30 13:15:34
我正在尝试使用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?
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 250 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信