Home > Web Front-end > HTML Tutorial > Get the ID name of the current button or html

Get the ID name of the current button or html

不言
Release: 2018-06-05 16:32:37
Original
2829 people have browsed it

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=&#39;width:70px;margin:5px;height:70px;&#39; id=&#39;"+num+"&#39; onclick=&#39;upimg()&#39; src=\"" + images.localId + "\" />");
Copy after login

Start getting it like this:

  function upimg(){
    var id= $(this).attr("id");
    alert(id);
    }
Copy after login

No. Later I used:

  function upimg(){
    alert(event.target.id);
    }
Copy after login

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!

Related labels:
id
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template