The following is an example of getting the ID name of the current button or html (recommended). It’s pretty good. I’ll share it with you now and give it as a reference.
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.
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
Related recommendations:
js method to get the value of the span tag in html (super Simple)
The above is the detailed content of Get the ID name of the current button or html. For more information, please follow other related articles on the PHP Chinese website!