在事件上使用jquery获取点击元素? [英] get clicked element using jquery on event?

查看:142
本文介绍了在事件上使用jquery获取点击元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新版本08-31 这是关闭但很受欢迎,所以我正在编辑希望清理一些东西。最初的问题实际上是缓存问题。我删除了所有这些,所以问题和答案在一起更加有意义,我接受了最高的答案。希望这对某人有用。



RE: jquery选择器单击不从动态生成的按钮触发?



我使用以下代码来检测动态生成的按钮被点击的时间。

  $(document).on click,。appDetails,function(){
alert(test);
});

通常,如果你刚刚执行 $('。appDetails'),点击()可以使用 $(this)来获取点击的元素。如何使用上述代码完成此操作?



例如:

  $(document).on(click,。appDetails,function(){
var clickedBtnID =
alert('you clicked on button#'+ clickedBtnID );
});


解决方案

简单,因为它可以是

使用 $(this)这里也是

  $(document).on(click,。appDetails,function(){
var clickedBtnID = $(this).attr('id'); //或var clickedBtnID = this .id
alert('你点击按钮#'+ clickedBtnID);
});


UPDATE 2016-08-31 This is closed but popular, so I'm editing to hopefully clean some things up. The original issue I actually had turned out to be a caching problem. I have removed all that so the question and answers make more sense together and I have accepted the top answer. Hopefully this will be useful to someone.

RE: jquery selector click not firing from dynamically generated button?

I'm using the following code to detect when a dynamically generated button is clicked.

$(document).on("click",".appDetails", function () {
    alert("test");
});

Normally, if you just did $('.appDetails').click() you could use $(this) to get the element that was clicked on. How would I accomplish this with the above code?

For instance:

$(document).on("click",".appDetails", function () {
    var clickedBtnID = ??????
    alert('you clicked on button #' + clickedBtnID);
});

解决方案

as simple as it can be

use $(this) here too

$(document).on("click",".appDetails", function () {
   var clickedBtnID = $(this).attr('id'); // or var clickedBtnID = this.id
   alert('you clicked on button #' + clickedBtnID);
});

这篇关于在事件上使用jquery获取点击元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆