jQuery/Javascript:改变一个锚定标签的onclick
jQuery/Javascript : Change onclick of an achor tag
我想改变锚的onclick
事件,但我无法使用javascript让它工作。这是我正在尝试的:document.getElementById("CommCo").onclick = "GetComments("+id+");";
如果只是属性的值,则使用
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);
});
通过javascript分配onclick
属性需要一个函数:
document.getElementById("CommCo").onclick = function() { GetComments(this.id); };
此处为工作示例。
如果你打算使用jQuery,下面是绑定事件的语法:
$('#CommCo').bind('click', function(e) {
// 'this' in here is the element that was clicked
GetComments(this.id);
});
-
'#CommCo'
是jquery的ID选择器 - 你也可以使用
.click()
方法,它内部调用.bind('click', ...)
尝试使用JQuery
var idvar = 1234; // Some ID
$("#CommCO").bind("click", {id:idvar}, function(event) { getComments(event.data.id); });
祝你好运!
jQuery中:
$('#CommCo').on('click', function() {
GetComments($(this).attr('id'));
});
问题是用jquery标记的所以,我将展示如何在这个框架中做到这一点:
$("#CommoCo").click( function() { GetComments( this.id ); } );
相关文章: