Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.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/0/amazon-s3/2.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
javascript对象在html页面上工作,但无法在wordpress中加载_Javascript_Wordpress_Object_Declaration - Fatal编程技术网

javascript对象在html页面上工作,但无法在wordpress中加载

javascript对象在html页面上工作,但无法在wordpress中加载,javascript,wordpress,object,declaration,Javascript,Wordpress,Object,Declaration,这段代码在html页面上运行良好。(标题) 当我试图在Wordpress页面上访问它时,这个javascript是使用主题函数加载到标题中的,控制台显示$(init)不是一个函数 Wordpress中这种对象声明方式有什么问题?这实际上与Wordpress本身无关,可能与未加载JavaScript代码/函数或变量的含义有关 请尝试以下操作: (function ($) { $(document).ready(function () { // ALL YOUR CODE HE

这段代码在html页面上运行良好。(标题)

当我试图在Wordpress页面上访问它时,这个javascript是使用主题函数加载到标题中的,控制台显示
$(init)
不是一个函数


Wordpress中这种对象声明方式有什么问题?

这实际上与Wordpress本身无关,可能与未加载JavaScript代码/函数或变量的含义有关

请尝试以下操作:

(function ($) {
    $(document).ready(function () {
       // ALL YOUR CODE HERE
    });
}(window.jQuery));
此代码将代码包装到一个函数中,该函数将
$
变量命名为jQuery,然后立即执行该函数。默认情况下,这将删除作为jQuery快捷方式的
$

$(document.ready
确保在DOM完全就绪之前不会执行代码

一个较短的方法可能是这样做:

jQuery( document ).ready(function( $ ) {
  // ALL YOUR CODE HERE
});

由于
startstop
应该是全局可用的,并且在标题中设置,因此您应该仅在触发
文档.ready
事件后调用
$(init)
。这应该在您的
starstop
函数中完成,这样您的
startstop
就可以在全球范围内使用(这是一种糟糕的做法,但您的代码可以工作)

同时将所有参考表格
$
更改为
jQuery
,以防万一

var startstop = new (function() {

    var $stopwatch, // Stopwatch element on the page
    incrementTime = 70, // Timer speed in milliseconds
    currentTime = 0, // Current time in hundredths of a second

    updateTimer = function() {
        $stopwatch.val(formatTime(currentTime));
        currentTime += incrementTime / 10;
    },

    init = function() {
        $stopwatch = jQuery('.stopwatch');
        startstop.Timer = jQuery.timer(updateTimer, incrementTime, true);
        startstop.Timer.stop();
    };

    this.resetStopwatch = function() {
        currentTime = 0;
        this.Timer.stop().once();
    };

    // Call this function when document.ready is triggered
    jQuery( document ).ready(function( $ ) {
        $(init);
    });
});

我一直在寻找这样的解决办法。当我包装文档准备就绪时调用init(),但随后我得到ReferenceError:startstop未定义startstop.Timer.toggle();在以前的jquery加载文件中定义:;(function($){….this.toggle=函数(reset){if(this.isActive){this.pause();}else if(reset){this.play(true);}else{this.play();}返回this;};…})(jQuery);然后您可能需要将
$(init)
绑定到
document.ready
事件。看新答案。解决了!我已经添加了documentready函数,然后我不得不将startstop变量声明移到documentready函数之外,这样它就可以全局访问。谢谢你的帮助豪尔赫·席尔瓦!
var startstop = new (function() {

    var $stopwatch, // Stopwatch element on the page
    incrementTime = 70, // Timer speed in milliseconds
    currentTime = 0, // Current time in hundredths of a second

    updateTimer = function() {
        $stopwatch.val(formatTime(currentTime));
        currentTime += incrementTime / 10;
    },

    init = function() {
        $stopwatch = jQuery('.stopwatch');
        startstop.Timer = jQuery.timer(updateTimer, incrementTime, true);
        startstop.Timer.stop();
    };

    this.resetStopwatch = function() {
        currentTime = 0;
        this.Timer.stop().once();
    };

    // Call this function when document.ready is triggered
    jQuery( document ).ready(function( $ ) {
        $(init);
    });
});