2014-08-30 37 views
-1

我試圖使用('點擊')方法。使用.on(「click」)獲取錯誤,但它使用jQuery中的.click()?

$('.pacotesDesc').on("click",function(){console.log("test")}) 
TypeError: undefined is not a function 

它並沒有工作。

但是當我使用。點擊()方法,它的工作原理:

$('.pacotesDesc').click(function(){console.log("test")}) 

爲什麼I'm得到一個錯誤,當我使用。對(「點擊」)的方法,但它的工作原理,當我使用。點擊()方法在jQuery中?

+2

你使用的是什麼jQuery版本? – 2014-08-30 05:17:00

+0

對Jquery 1.7存在和以上 – TGH 2014-08-30 05:17:47

+0

舊版本試試?嘗試委託或生活http://api.jquery.com/live/ – 2014-08-30 05:21:19

回答

3

我假設如果您使用舊版本的jQuery,然後根據jQuery;
http://api.jquery.com/live/

在jQuery 1.7的,所述.live()方法被棄用。使用.on()附加事件處理程序。老版本的jQuery用戶應優先使用.delegate(),而不要使用.live()。

$(selector).live(events, data, handler);    // jQuery 1.3+ 
$(document).delegate(selector, events, data, handler); // jQuery 1.4.3+ 
$(document).on(events, selector, data, handler);  // jQuery 1.7+ 

否則,您可以elobrate更多關於你的代碼,我會盡力幫助更多。

0

試試這個。

$(document).on("click", ".pacotesDesc", function(){ 
    alert("Oh noes, you clicked me"); 
});