jQuery小部件在Rails 4中消失了 [英] jQuery widgets disappearing in Rails 4
问题描述
我正在处理两个项目.一个使用jQuery Datepicker,另一个使用jQuery bxSlider.我两个都有同样的问题.当我导航到这些小部件所在的网址时,这些小部件不会出现.仅当我在浏览器中单击刷新时,它们才会出现.然后,当我导航到另一个链接并再次返回时,小部件又消失了,直到再次点击刷新.
I have two projects I'm working on. One has a jQuery Datepicker and the other uses the jQuery bxSlider. I have the same problem with both. When I navigate to the url where these widgets are located the widgets don't appear. Only when I hit refresh on my browser do they appear. Then when I navigate to another link and back again the widgets are gone again until I hit refresh another time.
编辑
我添加了以下内容:
$(document).on('ready', function(){
$('.slider').bxSlider({
slideWidth: 230,
minSlides: 1,
maxSlides: 3,
moveSlides: 1,
slideMargin: 10
});
});
推荐答案
您在Rails 4的新增功能Turbolinks中遇到了问题.您不能仅绑定到ready
事件,因为该事件仅在整页重新加载时调用(Turbolinks尽可能避免这种情况).您需要像这样绑定到page:change
事件:
You're having an issue with a new addition to Rails 4 called Turbolinks. You cannot just bind to the ready
event because that is only called on a full page reload(which Turbolinks avoids when possible). You need to bind to page:change
event like so:
$(document).on('page:change', function () {
$('.slider').bxSlider({
slideWidth: 230,
minSlides: 1,
maxSlides: 3,
moveSlides: 1,
slideMargin: 10
});
});
您可以在此处了解更多有关Turbo链接的信息.
You can read more about turbo links here.
这篇关于jQuery小部件在Rails 4中消失了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!