【问题标题】:Wordpress: Uncaught ReferenceError: myfunction is not definedWordpress:未捕获的 ReferenceError:myfunction 未定义
【发布时间】:2015-10-30 12:41:04
【问题描述】:

这是我在 wordpress 中排队的 javascript,但我有这个错误。 “未捕获的 ReferenceError:未定义我的函数”。

(function($) {
    function myfunction(bf) {
        if(bf.checked)
            var text1 = document.getElementById("shipping_first_name").value;
        else
            text1='';
        document.getElementById("billing_first_name").value = text1;
        };
})(jQuery);

我也试过这段代码。

(function($) {
    jQuery(document).ready(function($) {

        function myFunction(bf) {
            if(bf.checked)
                var text1 = document.getElementById("shipping_first_name").value;
            else
                text1='';

            document.getElementById("billing_first_name").value = text1;
            }
    });
})(jQuery);

入队:

function my_scripts_method() {
    wp_enqueue_script(
        'custom-script',
        get_template_directory_uri() . '/js/shipping.js',
        array( 'jquery' ),
        false,
        '1.0',
        true
    );
}

add_action( 'wp_enqueue_scripts', 'my_scripts_method' );

【问题讨论】:

  • 在 clouser 中定义的方法只能从 clouser 本身访问。 (function(){ /*** */ })

标签: jquery wordpress woocommerce referenceerror


【解决方案1】:

在闭包内部定义的方法只能在闭包内部访问。

            (function(){
                /* This is called closure
                 All code here is solely on clouser
                */
            }())

要访问该方法,您可以按如下方式更改闭包

            (function(){
                window.myfunction = function(bf){
                    /*...*/
                }
            }())

【讨论】:

  • 我不太明白,noconflict (})(jQuery);) 怎么样
  • 我猜你的意思是关闭,而不是关闭 :)
  • @AdriánVázquez 你的 noconflict 在哪里,我在你的代码中看不到任何 noconflict 用法。
  • 抱歉忘记了。我把 myFunction(bf) 这样的变量放在哪里?
猜你喜欢
  • 1970-01-01
  • 2016-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多