1
모든 페이지에 대해 단일 스크립트 파일을 갖고 싶습니다.이 스크립트에는 특정 페이지에 필요하지 않을 수도있는 스크립트가 있습니다. 이것이 내가이 오류가 발생합니다 (#select 요소가 나던) 홈페이지에서 실행하고 내 스크립트 나누기요소가 페이지에없는 경우 Javascript 오류
$(function() {
var $select = $('#select');
var $window = $(window);
var isFixed = false;
var init = $('#select').offset().top;
$window.scroll(function() {
var currentScrollTop = $window.scrollTop();
if (currentScrollTop > init && isFixed === false) {
isFixed = true;
$select.css({
top: 0,
position: 'fixed'
});
console.log("fixed");
} else if(currentScrollTop <= init && isFixed === true) {
isFixed = false;
$select.css('position', 'relative');
console.log("unfixed");
}
});
$("#scroll").click(function() {
$('html, body').animate({
scrollTop: $(".jumbo").offset().top + $(".jumbo").height()
}, 300);
});
});
그러나 때 :
Uncaught TypeError: Cannot read property 'top' of undefined
어떻게 내가 할 수있는 내 홈페이지에 나는이 스크립트를 가지고 해당 스크립트가 자체 페이지에로드하지 않고도 해당 페이지에서 필요하지 않으면 오류가 발생하지 않습니까?
페이지로드시 함수를 호출해야합니까?
'.length'는 갈 길입니다! :) –