如何在jQuery中应用单击以用于将来的元素?

时间:2015-07-28 14:42:31

标签: jquery

如何为将来的元素应用jquery单击?请参阅下面的代码

$(document.body).on('click', '.testClass', testfunction);

我想只允许单击此代码。我该怎么办?

提前致谢....

1 个答案:

答案 0 :(得分:3)

使用one()

$(document.body).one('click', '.testClass', testfunction);

这将为所有 .testClass元素触发一个点击事件。

如果您要点击一个点击事件 .testClass,请手动跟踪是否点击了data-*个属性

的元素
$(document.body).on('click', '.testClass', testfunction);

function testfunction(){
    if($(this).data('clicked')) return;
    $(this).data('clicked', 1);
    /* rest of logic there*/
}