我没有定义' navigateTo未定义'我的JS中的错误如下。我很确定我已将函数navigateTo
作为参数传递给openCart()
函数,因此不确定我哪里出错?
$(function() {
var form = $('form#checkout-form'),
$sections = $('[data-step]');
function navigateTo(index) {
$sections.removeClass('is--active').eq(index).addClass('is--active');
}
});
$(document).on('click', 'nav.main a.cart', function(e) {
openCart();
});
function openCart(navigateTo) {
navigateTo(1);
disableScroll = false;
}
答案 0 :(得分:1)
你的函数openCart
应该进入dom ready函数内部,否则它不会调用函数,还需要将回调作为参数navigateTo
传递给opencart
函数像openCart(navigateTo)
;
$(function() {
var form = $('form#checkout-form'),
$sections = $('[data-step]');
function navigateTo(index) {
alert(2);
$sections.removeClass('is--active').eq(index).addClass('is--active');
}
$(document).on('click', 'div', function(e) {
openCart(navigateTo);
});
function openCart(navigateTo) {
navigateTo(1);
disableScroll = false;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>hahah</div>
答案 1 :(得分:-1)
这是一个范围问题。将整个脚本包装在一个闭包中,你会没事的;
(function($) {
// your script here
})(jQuery)
但是也要摆脱第一个街区周围的$(function() { }
。