2012-05-29 117 views
91

全部, 我下載了一個預先捆綁的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"); 

然而,我仍然會有同樣的錯誤。有誰知道什麼會造成這個問題,以及如何解決這個問題?

在此先感謝!

回答

261

這是一個語法問題,WordPress中包含的jQuery庫以「無衝突」模式加載。這是爲了防止WordPress可以加載的其他JavaScript庫的兼容性問題。在「無confict」模式下,$快捷方式不可用,則使用較長的jQuery的,即

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

通過包括括號中的$後的函數調用,那麼您可以使用代碼塊中的此快捷方式。

有關詳情請參閱WordPress Codex

+0

我應該添加你需要在最後鬆開「jQuery」 – RedEyedMonster

+3

你是這樣一個生活的節省!經過3天的調試,我發現這是我的問題的確切解決方案。儘管WordPress正在加載jQuery文件,但我無法訪問文檔中提到的提示。所以這個代碼的確切代碼就是'jQuery(document).ready(function($){''固定好了,謝謝大家分享。 – Devner

+2

這也是Drupal中的同樣的問題,謝謝 – nextgtech

33

我最喜愛的無衝突型結構:

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

調用jQuery的一個函數指針是$(文件)。就緒(...)

或快捷,因爲我們說,在coffeescript:

jQuery ($) -> 
    # code here 
+0

如果'$'已經是一個jQuery實例 - 是否有任何理由傳遞'jQuery'並再次給它賦予'$'名字? – zerkms

+2

$可能不是jQuery實例,如果它與另一個庫衝突 - 不衝突模式。 – Julian

+4

這是$(document).ready()'的快捷方式,而不是'$(document).on('load')' –

-1

也許你有這樣的代碼了jQuery前:

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

,並將它們是衝突

,您可以更改$到(jQuery的)

+1

我用'var $ = jQuery.noConflict();'Mine是一個java-webapp安裝程序,但是我得到了同樣的錯誤! –

5

在WordPress的只是取代

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

jQuery(function(){...}); 
1

您可以考慮通過添加類似以下內容的主題functions.php文件,以取代谷歌圖書館WordPress默認jQuery腳本:從這裏取

function modify_jquery() { 
    if (!is_admin()) { 
     wp_deregister_script('jquery'); 
     wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js', false, '1.10.2'); 
     wp_enqueue_script('jquery'); 
    } 
} 
add_action('init', 'modify_jquery'); 

代碼:http://www.wpbeginner.com/wp-themes/replace-default-wordpress-jquery-script-with-google-library/

+0

最好的解決方案如果你使用外部的「外部」JQuery插件WP 缺點: - 可以 - 與一些WordPress插件衝突 - 沒有任何注意到我身邊 – RunsnbunsN

相關問題