Home >Backend Development >PHP Tutorial >Traverse img to get the current clicked img attribute

Traverse img to get the current clicked img attribute

WBOY
WBOYOriginal
2016-09-13 08:46:041097browse

Hello everyone, I am a little nervous to ask 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?

<code>{foreach from=$answers item=item key=key }
<img src="{$item.img}" num="{$key}" onclick="selectImg(this)" class="funny-img" 
style=" width: 23%;margin: 0 1% 4% 1%;float: left; opacity: 0.6;">     
{/foreach}

这是遍历的img</code>

Traverse img to get the current clicked img attribute

<code>
//打印当前元素
function selectImg(a){

    console.log(a)

}

</code>

Traverse img to get the current clicked img attribute

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

Statement:
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