2011-12-28 47 views
1

我想更改onclick事件的錨點,但無法使用javascript工作。 這是我想要的: document.getElementById("CommCo").onclick = "GetComments("+id+");";jQuery/Javascript:更改achor標記的onclick

編輯:對不起,但我要求改變它的價值,而不是事件處理程序本身。無論如何感謝你們所有人。

+0

是什麼意思喲 「變」?你想把它轉換成jQuery語法嗎? –

+1

您使用了'jquery'標籤,但是在代碼中您沒有使用任何有關jQuery的內容。 – Bipul

+0

_什麼是工作_是什麼意思?你期望會發生什麼?什麼不起作用? –

回答

3

如果它的屬性只是值,使用

document.getElementById("CommCo").setAttribute('onclick', 'GetComments('+id+');'); 

如果要更改'click'事件的事件處理程序,請執行

document.getElementById("CommCo").addEventListener('click', function() { 
    GetComments(this.id); 
}); 

如果你想這樣做jQuery的

$('#CommCo').bind('click', function (ev) { 
    GetComments(id); 
}); 
+0

謝謝。我想每個人都錯了我的問題。我要求改變價值而不是處理者。現在它完全改變! – xperator

1

分配通過JavaScript的onclick屬性需要一個函數:

document.getElementById("CommCo").onclick = function() { GetComments(this.id); }; 

工作示例here


如果你打算使用jQuery,這裏是結合事件的語法:

$('#CommCo').bind('click', function(e) { 

    // 'this' in here is the element that was clicked 

    GetComments(this.id); 

}); 
  • '#CommCo'是jQuery的
  • ID選擇,你還可以使用方法.click()這內部調用.bind('click', ...)
0

嘗試使用JQuery

var idvar = 1234; // Some ID 
$("#CommCO").bind("click", {id:idvar}, function(event) { getComments(event.data.id); }); 

祝你好運!

0

在jQuery中:

$('#CommCo').on('click', function() { 
    GetComments($(this).attr('id')); 
}); 
0

問題被標記與jQuery的話,我將展示如何做,在這個框架:

$("#CommoCo").click(function() { GetComments(this.id); });