使用rails 4和jQuery,但我似乎与两个函数有一些小的冲突。
第一个功能是滚动到顶部功能。
$(window).scroll(function(){
if ($(this).offset().top > 600) {
$('.scrollToTop').fadeIn(500);
} else {
$('.scrollToTop').fadeOut(500);
}
});
//Click event to scroll to top
$('.scrollToTop').click(function(){
$('html, body').animate({scrollTop : 0},800);
return false;
});
另一个是嵌套属性(RailsCast教程)函数。
$(function() {
$("form").on("click", ".remove_fields", function(event) {
$(this).prev("input[type=hidden]").val("1");
$(this).closest("fieldset").hide();
return event.preventDefault();
});
return $("form").on("click", ".add_fields", function(event) {
var regexp, time;
regexp = void 0;
time = void 0;
time = new Date().getTime();
regexp = new RegExp($(this).data("id"), "g");
$(this).before($(this).data("fields").replace(regexp, time));
return event.preventDefault();
});
});
现在一个可以工作,而另一个则不能,我不确定问题是什么。如果我剪切并重新粘贴嵌套属性 jQuery,它可以工作,但如果我单击滚动到它,它就不起作用。
我是 jQuery 新手,所以我不确定是否会出现某种冲突。
Application.js
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
最佳答案
按照此 gem 的步骤操作 https://github.com/kossnocorp/jquery.turbolinks但不要向下移动//= require Turbolinks 只需将其完全删除即可。
关于javascript - Rails 和 jQuery 有时不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29465289/