在事件上使用jquery获取点击元素? [英] get clicked element using jquery on event?
问题描述
更新版本08-31 这是关闭但很受欢迎,所以我正在编辑希望清理一些东西。最初的问题实际上是缓存问题。我删除了所有这些,所以问题和答案在一起更加有意义,我接受了最高的答案。希望这对某人有用。
我使用以下代码来检测动态生成的按钮被点击的时间。
$(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屋!