Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在wordpress中将JavaScript代码添加到js文件_Javascript_Php_Wordpress - Fatal编程技术网

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

如何在wordpress中将JavaScript代码添加到js文件,javascript,php,wordpress,Javascript,Php,Wordpress,如何在wordpress中将javascript代码添加到.js文件和function.php? 我有那个js代码,我将那个代码添加到一个.js文件中,并通过wp_enqueue_脚本函数在function.php中使用它,但它不会加载 $(document).ready(function () { // When user clicks on tab, this code will be executed $(".mtabs li").click(funct

如何在wordpress中将javascript代码添加到.js文件和function.php? 我有那个js代码,我将那个代码添加到一个.js文件中,并通过wp_enqueue_脚本函数在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");
        });
    });

请确保在wp_排队_脚本时使用jQuery依赖项

wp\u排队\u脚本'script',获取\u模板\u目录\u uri。 '/js/script.js',数组'jquery',false,false

另外,将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");
        });
    });

只是一种改进/不同的方法

在将jQuery依赖项与wp_enqueue_脚本(如此wp_enqueue_脚本“script”)一起使用后,获取_template_directory_uri/js/script.js',数组'jquery',1.1,true

您可以将jQuery封装在一个立即调用的函数表达式-Function{}中,而不是在整个代码中重复jQuery


这将传递jQuery作为参数,该参数将在函数中作为$使用。通过这种方式,您可以使用已经习惯的语法

在为wordpress:D:D:D编写javascript时,我完全停止使用$sign
(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);