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