我正在将megamenu从HTML / CSS / JavaScript转换为可在WordPress中工作。我创建了一个工作的助行器,一切就绪。问题是,我无法使JavaScript正常工作。JavaScript应该被触发,li
以便在单击时触发顶级菜单来打开超级菜单部分,并在再次单击时将其关闭。
我使用了以下JavaScript文件:
var swMegaMenu = (function() {
var $listItems = $( '#sw-hrmenu > ul > li' ),
$menuItems = $listItems.children( 'a' ),
$body = $( 'body' ),
current = -1;
function init() {
$menuItems.on( 'click', open );
$listItems.on( 'click', function( event ) { event.stopPropagation(); } );
}
function open( event ) {
if( current !== -1 ) {
$listItems.eq( current ).removeClass( 'sw-hropen' );
}
var $item = $( event.currentTarget ).parent( 'li' ),
idx = $item.index();
if( current === idx ) {
$item.removeClass( 'sw-hropen' );
current = -1;
}
else {
$item.addClass( 'sw-hropen' );
current = idx;
$body.off( 'click' ).on( 'click', close );
}
return false;
}
function close( event ) {
$listItems.eq( current ).removeClass( 'sw-hropen' );
current = -1;
}
return { init : init };
})();
我已经将它插入到footer.php中:
<script>
$(function() {
swMegaMenu.init();
});
</script>
问题是我在footer.php中收到此错误:
<script>
$(function() { // Uncaught TypeError: Undefined is not a function
swMegaMenu.init();
});
</script>
以及JavaScript文件中的以下错误:
var swMegaMenu = (function() {
var $listItems = $( '#sw-hrmenu > ul > li' ), // Uncaught TypeError: Undefined is not a function
$menuItems = $listItems.children( 'a' ),
$body = $( 'body' ),
current = -1;
您正在使用WordPress的默认jQuery实例,但没有使用noConflict()包装器。
在该noConflict()
模式下,$
jQuery的全局快捷方式不可用。这就是为什么您看到一个undefined
错误。
要解决此问题,需要来替换的所有实例$
用jQuery
,或用包装材料包住整组的功能。例如:
(function($) {
// Inside of this function, $() will work as an alias for jQuery()
// and other libraries also using $ will not be accessible under this shortcut
})(jQuery);
在Codex中阅读更多内容。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句