2012-12-10 36 views
0

+0

您爲其他問題提供的代碼會將點擊處理程序綁定到任何元素類型,包括'

回答

1

請你參考下面的代碼。

$("button#buttonAnswer1").on("click",hello); 

請嘗試的jsfiddle上面的代碼,讓我知道你的顧慮。

http://jsfiddle.net/

1

你想選擇所有<button>與數據線設置爲true

如果是這樣,試試這個:http://jsfiddle.net/M9pwp/2/

HTML:

<button id="buttonAnswer1" data-inline="true">Click me</button> 

腳本:

$("button").click(function() { 
    if ($(this).attr('data-inline') == 'true') 
     hello(); 
}); 

function hello() 
{ 
    alert('hi'); 
} 

如果你只是想爲這個特定的按鈕監聽器,則可以更改腳本到:

$("#buttonAnswer1").click(function() { 
    hello(); 
}); 

function hello() 
{ 
    alert('hi'); 
}