我知道当事件触发如下
时,我知道要通过ID的JavaScript<td><a id="applyChange_${(loop.index)}" onclick="javascript:applyChange(this);">
function applyChange(obj){
console.log(obj.id;) // this returns the id of the element
}
但是,如何用jQuery写同样的?
添加class 到您的 a
标签和删除内联事件binder
<td><a id="applyChange_${(loop.index)}" class="applyChange">
使用此结构,您可以在类上编写 jQuery选择器,然后单击与类applyChange
的任何元素,您可以通过使用$(this).attr('id')
id
所以脚本就像
$('.applyChange').on('click',function(){
console.log($(this).attr('id'));
});
如果您只想获得索引,我会做这样的事情:
<a class="applyChange" data-id="${(loop.index)}">
然后在页面加载时设置单击事件(假设这些事件是动态创建的)
$(document).on('click', '.applyChange', function () { console.log($(this).data('id')) });