특정 버튼을 숨기고 표시하는 간단한 스크립트가 있지만 페이지가 다시로드 될 때까지는 JavaScript가 실행되지 않습니다. 사용자가 링크를 클릭하자마자 실행하려면 어떻게해야합니까? 예를 들어 사용자가 처음 페이지에 링크하면 특정 div가 이미 숨겨집니다.JQuery가 페이지 새로 고침까지 실행되지 않습니다.
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 준비 처리기에 코드를 넣었습니까? – karthikr
이 문제는 JQuery와 Turbolinks와 관련이 있습니다. – CPride
[Rails 4 turbo-link는 jQuery 스크립트가 작동하지 못하게합니다.] (http://stackoverflow.com/questions/18769109/rails-4-turbo-link-prevents-jquery-scripts-from-working) –