Wordpress儿童主题不通过JQuery应用风格

时间:2021-08-26 07:44:23

I am enqueueing script which applies the class "first-half" to the first half of all the li elements under ul#menu-main. The class is not showing.

我将队列“前半部分”应用到ul#menu-main下所有li元素的前半部分。班级没有显示。

What I have:

是)我有的:

In "themes/responsive-child/js/menu.js":

$(document).ready(function(){

    var menuItems = $("ul#menu-main li");

    menuItems = menuItems.slice(0, Math.floor(menuItems.length/2)).addClass("first-half");

});

In "functions.php" of child theme:

在儿童主题的“functions.php”中:

add_action('init', 'menuscript_init');

function menuscript_init() {
    wp_enqueue_script( 'menuscript_init', '<?php get_stylesheet_directory_uri() ?>/js/menu.js'); 
}

Please point out what I did wrong

请指出我做错了什么

3 个解决方案

#1


0  

change

<?php get_stylesheet_directory() ?>

to

<?php get_template_directory() ?>

#2


0  

I can help you. Your problem is in functions.php, you have to have something like:

我能帮你。你的问题出在functions.php中,你必须得到类似的东西:

wp_register_script( 'editing_unit_javascript', get_stylesheet_directory_uri().'/js/menu.js', array('jquery','bootstrap'));
wp_enqueue_script( 'menu_javascript' );

EDIT: You can remove 'bootstrap' from array, because you are going to use jquery in your javascript file

编辑:您可以从数组中删除'bootstrap',因为您将在javascript文件中使用jquery

#3


0  

change jquery

jQuery(document).ready(function(){

    var menuItems = jQuery("ul#menu-main li");

    menuItems = menuItems.slice(0, Math.floor(menuItems.length/2)).addClass("first-half");

});

and change wp_enqueue_script() to :

并将wp_enqueue_script()更改为:

wp_enqueue_script( 'menuscript_init', get_stylesheet_directory_uri().'/js/menu.js', array('jquery'));

hope it will work for you.

希望它能为你效劳。

#1


0  

change

<?php get_stylesheet_directory() ?>

to

<?php get_template_directory() ?>

#2


0  

I can help you. Your problem is in functions.php, you have to have something like:

我能帮你。你的问题出在functions.php中,你必须得到类似的东西:

wp_register_script( 'editing_unit_javascript', get_stylesheet_directory_uri().'/js/menu.js', array('jquery','bootstrap'));
wp_enqueue_script( 'menu_javascript' );

EDIT: You can remove 'bootstrap' from array, because you are going to use jquery in your javascript file

编辑:您可以从数组中删除'bootstrap',因为您将在javascript文件中使用jquery

#3


0  

change jquery

jQuery(document).ready(function(){

    var menuItems = jQuery("ul#menu-main li");

    menuItems = menuItems.slice(0, Math.floor(menuItems.length/2)).addClass("first-half");

});

and change wp_enqueue_script() to :

并将wp_enqueue_script()更改为:

wp_enqueue_script( 'menuscript_init', get_stylesheet_directory_uri().'/js/menu.js', array('jquery'));

hope it will work for you.

希望它能为你效劳。