92

すべて、プリバンドルされた JS/CSS フォーム アプリケーションをダウンロードし、それを Wordpress で使用しようとしています。私は次のコードを持っています:

$(document).ready(function () {


/*----------------------------------------------------------------------*/
/* Parse the data from an data-attribute of DOM Elements
/*----------------------------------------------------------------------*/


$.parseData = function (data, returnArray) {
    if (/^\[(.*)\]$/.test(data)) { //array
        data = data.substr(1, data.length - 2).split(',');
    }
    if (returnArray && !$.isArray(data) && data != null) {
        data = Array(data);
    }
    return data;
};

/*----------------------------------------------------------------------*/
/* Image Preloader
/* http://engineeredweb.com/blog/09/12/preloading-images-jquery-and-javascript
/*----------------------------------------------------------------------*/



// Arguments are image paths relative to the current page.
$.preload = function() {
    var cache = [],
        args_len = arguments.length;
    for (var i = args_len; i--;) {
        var cacheImage = document.createElement('img');
        cacheImage.src = arguments[i];
        cache.push(cacheImage);
    }
};


/*----------------------------------------------------------------------*/
/* fadeInSlide by revaxarts.com
/* Fades out a box and slide it up before it will get removed
/*----------------------------------------------------------------------*/


$.fn.fadeInSlide = function (speed, callback) {
    if ($.isFunction(speed)) callback = speed;
    if (!speed) speed = 200;
    if (!callback) callback = function () {};
    this.each(function () {

        var $this = $(this);
        $this.fadeTo(speed / 2, 1).slideDown(speed / 2, function () {
            callback();
        });
    });
    return this;
};


/*----------------------------------------------------------------------*/
/* fadeOutSlide by revaxarts.com
/* Fades out a box and slide it up before it will get removed
/*----------------------------------------------------------------------*/


$.fn.fadeOutSlide = function (speed, callback) {
    if ($.isFunction(speed)) callback = speed;
    if (!speed) speed = 200;
    if (!callback) callback = function () {};
    this.each(function () {

        var $this = $(this);
        $this.fadeTo(speed / 2, 0).slideUp(speed / 2, function () {
            $this.remove();
            callback();
        });
    });
    return this;
};

/*----------------------------------------------------------------------*/
/* textFadeOut by revaxarts.com
/* Fades out a box and slide it up before it will get removed
/*----------------------------------------------------------------------*/


$.fn.textFadeOut = function (text, delay, callback) {
    if (!text) return false;
    if ($.isFunction(delay)) callback = delay;
    if (!delay) delay = 2000;
    if (!callback) callback = function () {};
    this.each(function () {

        var $this = $(this);
        $this.stop().text(text).show().delay(delay).fadeOut(1000,function(){
            $this.text('').show();
            callback();
        })
    });
    return this;
};

/*----------------------------------------------------------------------*/
/* leadingZero by revaxarts.com
/* adds a leding zero if necessary
/*----------------------------------------------------------------------*/


$.leadingZero = function (value) {
    value = parseInt(value, 10);
    if(!isNaN(value)) {
        (value < 10) ? value = '0' + value : value;
    }
    return value;
};


});

Wordpress の競合がないことが問題の原因であると想定していたので、最後のブラケットを次のように更新しました。

}, "jQuery");

ただし、まだ同じエラーが発生します。この問題の原因と解決方法を知っている人はいますか?

前もって感謝します!

4

5 に答える 5

260

これは構文の問題です。WordPress に含まれる jQuery ライブラリは「競合なし」モードで読み込まれます。これは、WordPress がロードできる他の JavaScript ライブラリとの互換性の問題を防ぐためです。「非競合」モードでは、$ ショートカットは使用できず、jQuery がより長く使用されます。

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

関数呼び出しの後に括弧で $ を含めることにより、コード ブロック内でこのショートカットを使用できます。

詳細については、WordPress Codexを参照してください。

于 2012-05-29T21:53:41.700 に答える
35

私のお気に入りの非競合に優しい構造:

jQuery(function($) {
  // ...
});

関数ポインターを使用して jQuery を呼び出すことは、$(document).read(...) のショートカットです。

または、coffeescript で次のように言います。

jQuery ($) ->
  # code here
于 2012-05-29T21:53:01.903 に答える
6

Wordpressで置き換えるだけ

$(function(){...});

jQuery(function(){...});
于 2013-01-30T06:58:24.383 に答える
-1

jqueryの前に次のようなコードがあるかもしれません:

var $jq=jQuery.noConflict();
$jq('ul.menu').lavaLamp({
    fx: "backout", 
    speed: 700
});

そして彼らは紛争でした

$ を (jQuery) に変更できます

于 2012-11-03T23:27:18.147 に答える