Javascript Rails和jQuery有时不工作
使用Rails4和jQuery,但我似乎在两个函数之间有一些小冲突 第一个功能是滚动到顶部功能Javascript Rails和jQuery有时不工作,javascript,jquery,ruby-on-rails,ruby-on-rails-4,Javascript,Jquery,Ruby On Rails,Ruby On Rails 4,使用Rails4和jQuery,但我似乎在两个函数之间有一些小冲突 第一个功能是滚动到顶部功能 $(window).scroll(function(){ if ($(this).offset().top > 600) { $('.scrollToTop').fadeIn(500); } else { $('.scrollToTop').fadeOut(500); } }); //Click event to scroll to to
$(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;
});
$(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();
});
});
另一个是嵌套属性(RailsCast教程)函数
$(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;
});
$(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的步骤操作,但不要向下移动//=require turbolinks,只需将其完全移除。您应该使用gem 将
gem'jquery turbolinks'
添加到您的gem文件中,然后运行bundle安装
然后修改application.js
文件
//= require jquery
//= require jquery.turbolinks
//= require jquery_ujs
//
// ... your other scripts here ...
//
//= require turbolinks
首先,安装
jquery turbolinks
gem。然后,不要忘记将包含的Javascript文件从应用程序.html.erb
主体的末尾移动到它的
如果出于速度优化的原因将应用程序javascript链接放在页脚中,则需要将其移动到标记中,以便在加载标记中的内容之前加载。这个解决方案对我有效