Home >Web Front-end >JS Tutorial >Analyze how to get the ID name of the current button or html
What I did today is upload a picture, click on the picture to delete it.
Randomly give the picture id, get the picture id, and then delete the picture.
Since the image ID is random, clicking on the img or clicking on the class will not work to get the ID. Finally, use the onclick event to obtain it.
The js code is as follows:
$("#pic").append("<img style='width:70px;margin:5px;height:70px;' id='"+num+"' onclick='upimg()' src=\"" + images.localId + "\" />");
Start getting it like this:
function upimg(){var id= $(this).attr("id"); alert(id); }
No. Later I used:
function upimg(){ alert(event.target.id); }
to get it.
Summary:
Try to use event.target.id
, do not use this. id
.
When using event delegation, this
points to the element bound to listen for the event, not the clicked element. event.target.id
always points to the clicked element
The above is the detailed content of Analyze how to get the ID name of the current button or html. For more information, please follow other related articles on the PHP Chinese website!