2012-04-19 17 views
1

我有這個按鈕:使用原型來設置按鈕的onclick事件?

<button id="check_button" name ="check_button" type="button" onclick="setLocation('...')"> 
    <span>check</span> 
</button> 

現在我知道我可以用$( 'check_button'),例如訪問它。有沒有辦法用Prototype設置setlocation參數?

謝謝!

回答

2
function doButtonClick(event, element) { 
    console.log('event'); 
    console.log('element'); 

    // here, `element` is a reference to the button you clicked. 
    var elementId = element.identify(); 

    // I'm not sure what setLocation takes in as a parameter, but... 
    setLocation(elementId); 
} 

$('check_button').on('click', 'button', doButtonClick); 

這裏是爲element.on文檔:http://api.prototypejs.org/dom/Element/prototype/on/

這實在是創建一個新的Event.Handler的只是一個快速的方法:http://api.prototypejs.org/dom/Event/on/

爲了確保檢查出第二文檔鏈接 - 超級有用的東西。

0

是,

$( 'check_button')觀察( '點擊',this.setLocation.bind(這一點, '...'));

setLocation: function(param) 
{ 
alert(param); 
} 

這樣帕拉姆將永遠是你通過什麼。 「綁定」是一種持有該能力的變化強大的方法。