【发布时间】:2014-06-05 02:08:42
【问题描述】:
我有一个简单的脚本来隐藏和显示某些按钮,但是在重新加载页面之前,javascript 不会运行。一旦用户单击该链接,我怎样才能让它运行?例如,当用户第一次链接到页面时,页面是否已经隐藏了某些 div。
var array = [".section-1", ".section-2", ".section-3", ".section-4", ".section-5"];
var cnt = 0;
$(function() {
for(var i = 0; i < 5; i++) {
$(array[i]).hide();
}
$(array[cnt]).show();
$(".previous").hide();
$(".next").click(function () {
if(cnt < 4){
$(array[cnt]).hide();
cnt++;
$(array[cnt]).show();
}
if (cnt == 4) {
$(".next").hide();
}
if(cnt > 0) {
$(".previous").show();
}
});
$(".previous").click(function (){
if(cnt > 0) {
$(array[cnt]).hide();
cnt--;
$(array[cnt]).show();
}
if(cnt < 4){
$(".next").show();
}
if(cnt == 0){
$(".previous").hide();
}
});
});
这是清单文件。
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require users
【问题讨论】:
-
你把你的代码放在 DOM 就绪处理程序中了吗?
-
找出问题所在,与 JQuery 和 Turbolinks 有关。
标签: javascript jquery html ruby ruby-on-rails-4