1024programmer Java javascript-Traverse img to get the current clicked img attribute

javascript-Traverse img to get the current clicked img attribute

Hello everyone, I am a little nervous about asking questions for the first time. I am a novice and hope to make progress together with everyone. The problem is this. I traverse a group of img and want to get the subscript of the currently clicked img. I added a num attribute to the img to get the key of the element. How should I get the num of the current img?

{foreach from=$answers item=item key=key }
 
 {/foreach}

 This is the traversed img


 //Print the current element
 function selectImg(a){

     console.log(a)

 }

 

I found that console.log(this.num) is undefined. I have tried various methods. Now I don’t know how to get it. Please give me some guidance.

Reply content:

Hello everyone, I am a little nervous about asking questions for the first time. I am a novice and hope to make progress together with everyone. The problem is this. I traverse a group of img and want to get the subscript of the currently clicked img. I added a num attribute to the img to get the key of the element. How should I get the num of the current img?

{foreach from=$answers item=item key=key }
 
 {/foreach}

 This is the traversed img


 //Print the current element
 function selectImg(a){

     console.log(a)

 }

 

I found that console.log(this.num) is undefined. I have tried various methods. Now I don’t know how to get it. Please give me some guidance.

this.getAttribute(“num”);
Try this

If you don’t understand the principle, just use jq to bind a click event to the img tag. Get this which is the img you clicked. If you want to get whatever you want, use this.getAttribute(“attribute”);

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/719561

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索