使用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 .发布于 2015-04-06 04:22:57
按照这个gem https://github.com/kossnocorp/jquery.turbolinks的步骤进行操作,但是不要向下移动//=,只需将其完全删除即可。
发布于 2015-04-06 04:26:15
您应该使用jquery-turbolinks gem。
将gem 'jquery-turbolinks'添加到Gemfile中,然后运行bundle install。
然后修改您的application.js文件
//= require jquery
//= require jquery.turbolinks
//= require jquery_ujs
//
// ... your other scripts here ...
//
//= require turbolinks发布于 2016-06-21 10:03:59
首先,安装jquery-turbolinks gem。然后,不要忘记将包含的Javascript文件从application.html.erb正文的末尾移到它的<head>。
如果出于速度优化的原因,将应用程序javascript链接放置在页脚中,则需要将其移动到标记中,以便在标记中的内容之前加载。这个解决方案对我有效。
https://stackoverflow.com/questions/29465289
复制相似问题