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

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

问题描述

UPDATE 2016-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 );
});


解决方案

b
$ b

使用 $(this)此处

  $(document).on(click,。appDetails,function(){
var clickedBtnID = $(this).attr('id'); //或var clickedBtnID = this .id
alert('you clicked on button#'+ 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天全站免登陆