0
我使用硒插件以及我的插件中的jQuery。由於使用jquery函數$正在使用硒投擲函數未找到錯誤。刪除Jquery,一切正常。使用jquery(ajax調用)對我來說是必須的。請建議我如何讓他們一起工作。使用美元別名爲jQuery與硒
我使用硒插件以及我的插件中的jQuery。由於使用jquery函數$正在使用硒投擲函數未找到錯誤。刪除Jquery,一切正常。使用jquery(ajax調用)對我來說是必須的。請建議我如何讓他們一起工作。使用美元別名爲jQuery與硒
一個recommended way解決這種衝突是包裝你的JavaScript代碼在函數內部,並通過jQuery
作爲參數傳遞給這個函數:
// e.g : turn this code :
$(function(){
$('.my-class').on('click', function(){
$.ajax(...);
});
...
});
// into :
(function($) { // start an anonymous function,
// whose first argument is named '$' ...
$(function(){
$('.my-class').on('click', function(){
$.ajax(...);
});
...
});
}(jQuery)); // and call this function right away,
// passing the jQuery object as first argument
注意$
僅僅是一個快捷方式jQuery
:
jQuery('.my-class') and jQuery.ajax(...)
// are exactly the same as :
$('.my-class') and jQuery.ajax(...)
你也可以用你自己的別名:
var $j = jQuery;
如果有一天你需要定義一個$
可變另一個庫混合jQuery
,你也可以使用jQuery.noConflict()
(從this use case採取爲例):在調用jQuery的
var $j = jQuery.noConflict();
錯誤沒有被拋出。在硒函數調用期間發生錯誤。我試圖用jquery替換$。它沒有工作。實際上,我需要一種方法,以便硒中的$不被識別爲Jquery。 –
jQuery.noConflict()是要走的路。 –