jQuery插件选择器访问范围
jQuery Plugin selector access scope
我正在尝试访问下面闭包中jQuery对象的选择器,这样我就不必指定和/或缓存它。如果我用$this替换$(".the_lead"),它将不会执行它的操作。
调用插件
$(".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
。窗口滚动事件或多或少会激发用户移动的每个像素。如果没有看到更多插件的代码,我无法确定,但我敢打赌,只有当滚动事件第一次触发时,这才是正确的。
此外,缓存似乎只想缓存.the_lead元素中的一个,不是吗?这就是代码的读取方式。使用$this = $(this);
,但在函数中,您似乎想要类.the_lead
的所有元素。试着像这样直接缓存它$this=$('.the_lead');
相关文章:
- 可以't访问JavaScript函数范围中的变量
- 如何从chrome扩展访问angularjs根范围
- 访问内部功能范围的元素,而不是敲除中的外部
- 将 NodeJS 模块范围变量作为对象访问
- 主干.js(具有 Require.js)变量/范围访问问题
- AngularJS:从控制器访问特定的指令范围
- Angularjs:访问范围变量数组并计算平均值
- 如何访问嵌套的表单ng包含范围/字段
- 有棱角的如何获得ng重复项范围的访问权限
- 我怎么把范围弄错了?无法从jQuery访问此函数;.在“上”;
- webGL,javascript:尝试访问属性1中超出范围的顶点
- 是否有任何方法可以访问外部范围's变量
- 淘汰赛:can't从嵌套foreach访问父级范围的属性
- WebDriver Selenium:Javascript代码覆盖范围,无需服务器访问
- JS范围访问
- 从不同范围访问较新版本的变量(JavaScript / jQuery)
- 从外部绑定范围访问knockoutjs ViewModel
- 使用指令将具有范围访问权限的功能附加到html元素
- angular 1.5无范围访问http.get中的数据
- 从其他范围访问对象(javascript)