admin管理员组文章数量:1122832
This is the code.
jQuery(document).ready(function() {
jQuery( '#menu-item-927' ).on( 'ubermenuopen', function(){
jQuery('.site-content').addClass('blur');
});
});
It works when I use it in the console but does not work when in a JS file.
Here are the things I have tried:
- Enqueued script with jQuery dependency
wp_enqueue_script( 'script', get_stylesheet_directory_uri() . '/custom.js', array( 'jquery' ), '1.0.0', false );
and custom.js does show up in the source. - Added
jQuery(document).ready(function()
.
P.S: ubermenuopen is an events API from the UberMenu plugin.
This is the code.
jQuery(document).ready(function() {
jQuery( '#menu-item-927' ).on( 'ubermenuopen', function(){
jQuery('.site-content').addClass('blur');
});
});
It works when I use it in the console but does not work when in a JS file.
Here are the things I have tried:
- Enqueued script with jQuery dependency
wp_enqueue_script( 'script', get_stylesheet_directory_uri() . '/custom.js', array( 'jquery' ), '1.0.0', false );
and custom.js does show up in the source. - Added
jQuery(document).ready(function()
.
P.S: ubermenuopen is an events API from the UberMenu plugin.
Share Improve this question asked Mar 25, 2021 at 1:33 Stanley TanStanley Tan 1951 gold badge1 silver badge10 bronze badges 7 | Show 2 more comments1 Answer
Reset to default 0I tested and works fine. but you can use the wp_footer
hook and you can add your custom js. check below code. code will go to the active theme functions.php file.
function add_custom_scripts(){ ?>
<script type="text/javascript">
(function($){
$(document).ready(function() {
$( '#menu-item-927' ).on( 'ubermenuopen', function(){
$('.site-content').addClass('blur');
});
});
})(jQuery);
</script>
<?php }
add_action( 'wp_footer', 'add_custom_scripts', 10, 1 );
本文标签: jQuery works in console but not when in a file
版权声明:本文标题:jQuery works in console but not when in a file 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736287458a1927884.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
alert( 'custom.js' )
in the file and see if you get the alert. Also, there might be other dependencies that you must specify, e.g.ubermenu
? – Sally CJ Commented Mar 25, 2021 at 2:39alert( 'Load me!' );
in the custom.js file and the alert did show up. – Stanley Tan Commented Mar 25, 2021 at 4:13ubermenu
as a dependency (array( 'jquery', 'ubermenu' )
), but you'll need to identify the correct script handle like the 'script' in yourwp_enqueue_script()
code. You could also try enqueueing your script in the footer - set the fifth parameter totrue
. – Sally CJ Commented Mar 25, 2021 at 18:59ubermenu
dependency worked! Thank you! – Stanley Tan Commented Mar 31, 2021 at 4:26