繁体   English   中英

jQuery插件选择器访问范围

[英]jQuery Plugin selector access scope

我正在尝试在下面的闭包内部访问jQuery对象的选择器,以便不必指定它和/或对其进行缓存。 如果我将$(“。the_lead”)替换为$ this,它将无法执行操作。

调用插件

$(".the_lead").scroll_lead({speedup: 400});

var $this = $(this);

$(window).scroll(function() {

    var window_height = $(window).height();
    var document_height = $(document).height();
    var hide_lead;
    var scrollTop = $(window).scrollTop();

    console.log($this);

    if(!hide_lead){

        if(scrollTop>(document_height/2)){
            $(".the_lead").slideDown(options.speedup);
            }else{
            $(".the_lead").slideUp(500,function(){
                $(".the_lead").hide();
          });}
         }


        $('#hide_lead').click(function(e){
            //$(".the_lead").parent().parents('div').hide();
            hide_lead = true;           
            e.preventDefault();
        });     

    }); 

$(this)的控制台输出:

[selector: ".the_lead", context: document, constructor: function, init: function, selector: ""…]
context: #document
length: 0
selector: ".the_lead"
__proto__: Object[0]

请注意length: 0控制台输出为length: 0 窗口滚动事件或多或少会触发用户移动的每个像素。 我不确定自己没有看到更多插件的代码,但我敢打赌,只有在scroll事件首次触发时,$ this才是正确的。

此外,缓存似乎只希望缓存.the_lead元素之一,不是吗? 这就是您的代码读取的方式。 其中$this = $(this); 但是在函数中,您似乎想要所有带有.the_lead类的.the_lead 尝试像这样直接缓存它。 $ this = $('。the_lead');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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