简体   繁体   English

如何在Wordpress中将JavaScript代码添加到js文件

[英]How add JavaScript code to a js file in wordpress

How add javascript code to a .js file and add to function.php in wordpress? 如何将JavaScript代码添加到.js文件并在wordpress中添加到function.php? I have that js code and I add that code to a .js file and use that in function.php by wp_enqueue_script() function but It doesn't load. 我有该js代码,并将该代码添加到.js文件中,并通过wp_enqueue_script()函数在function.php中使用了该代码,但未加载。

 $(document).ready(function () {
        //  When user clicks on tab, this code will be executed
        $(".mtabs li").click(function () {
            //  First remove class "active" from currently active tab
            $(".mtabs li").removeClass('active');

            //  Now add class "active" to the selected/clicked tab
            $(this).addClass("active");

            //  Hide all tab content
            $(".mtab_content").hide();

            //  Here we get the href value of the selected tab
            var selected_tab = $(this).find("a").attr("href");

            //  Show the selected tab content
            $(selected_tab).fadeIn();

            //  At the end, we add return false so that the click on the link is not executed
            return false;
        });
        $("#simulate").click(function () {
            $('a[rel="tab2"]').trigger("click");
        });
    });

Just an improvement/different approach to @mash-r's answer 只是对@ mash-r答案的改进/不同方法

After using jQuery dependency with wp_enqueue_script like this wp_enqueue_script( 'script', get_template_directory_uri() . '/js/script.js', array ( 'jquery' ), 1.1, true); 在将jQuery依赖项与wp_enqueue_script一起使用后,像这样wp_enqueue_script( 'script', get_template_directory_uri() . '/js/script.js', array ( 'jquery' ), 1.1, true);

Instead of repeating jQuery all over your code you can wrap it in an IIFE (Immediately Invoked Function Expression - (function(){})(); ) 您可以将其包装在IIFE (立即调用的函数表达式- (function(){})(); )中,而不是在整个代码中重复使用jQuery (function(){})();

(function ($) {
    $(document).ready(function () {
        $(".mtabs li").click(function () {
            //  First remove class "active" from currently active tab
            $(".mtabs li").removeClass('active');

            //  Now add class "active" to the selected/clicked tab
            $(this).addClass("active");

            //  Hide all tab content
            $(".mtab_content").hide();

            //  Here we get the href value of the selected tab
            var selected_tab = $(this).find("a").attr("href");

            //  Show the selected tab content
            $(selected_tab).fadeIn();

            //  At the end, we add return false so that the click on the link is not executed
            return false;
        });
        $("#simulate").click(function () {
            $('a[rel="tab2"]').trigger("click");
        });
    });
})(jQuery);

This will pass jQuery as parameter, which will be used within the function as $ . 这将传递jQuery作为参数,该函数将在$ This way you can use the syntax you are already used to. 这样,您可以使用已经习惯的语法。

Make sure you use jQuery dependency when wp_enqueue_script. 确保在wp_enqueue_script时使用jQuery依赖关系。

wp_enqueue_script('script', get_template_directory_uri() . '/js/script.js', array ( 'jquery' ) , false, false); wp_enqueue_script('script',get_template_directory_uri()。'/js/script.js', array ( 'jquery' ) ,false,false);

Also, change all $ to jQuery in your javascript code. 另外,在您的JavaScript代码中将所有$更改为jQuery

jQuery(document).ready(function () {
        //  When user clicks on tab, this code will be executed
        jQuery(".mtabs li").click(function () {
            //  First remove class "active" from currently active tab
            jQuery(".mtabs li").removeClass('active');

            //  Now add class "active" to the selected/clicked tab
            jQuery(this).addClass("active");

            //  Hide all tab content
            jQuery(".mtab_content").hide();

            //  Here we get the href value of the selected tab
            var selected_tab = jQuery(this).find("a").attr("href");

            //  Show the selected tab content
            jQuery(selected_tab).fadeIn();

            //  At the end, we add return false so that the click on the link is not executed
            return false;
        });
        jQuery("#simulate").click(function () {
            jQuery('a[rel="tab2"]').trigger("click");
        });
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM