我使用以下.这不适用于动态创建的元素.我是jQuery 1.4.2
$(".wrapper1").live("scroll",function(){
alert(123);
$(".wrapper2")
.scrollLeft($(".wrapper1").scrollLeft());
});
Run Code Online (Sandbox Code Playgroud)
这也适用于普通元素.(在页面加载时加载)
这可能是什么原因.请帮助我.谢谢...
浏览器更改,jQuery错误是固定的,这就是为什么始终使用最新版本的jQuery很重要的两个原因(经过适当的测试后,你不能只指向最新版本).
你的代码适用于jQuery 1.9,对于另一种事件类型,
$(document).on("event_type",".wrapper1", function(){
$(".wrapper2")
.scrollLeft($(".wrapper1").scrollLeft());
});
Run Code Online (Sandbox Code Playgroud)
使用$(document)接收器的原因$(".wrapper1")是,只有绑定时存在的元素才会接收和委托事件.on不像旧的那样工作live.
除非这不会对scroll事件起作用,因为它们不会冒泡.
所以我可以提出的最合理的解决方案是定义一个函数:
$.fn.bindScrollHandler1 = function(){
$(this).on('scroll', function(){
$(".wrapper2").scrollLeft($(".wrapper1").scrollLeft());
});
}
Run Code Online (Sandbox Code Playgroud)
并在开始时调用它:
$('.wrapper1').bindScrollHandler1();
Run Code Online (Sandbox Code Playgroud)
每次创建一个新的.wrapper1元素时:
myNewElement.bindScrollHandler1();
Run Code Online (Sandbox Code Playgroud)
请注意,您的完整逻辑似乎有点缺乏,因为您不对滚动条进行配对,但使它们的工作方式相同.