开发者

Get attribute of clicked link with JS function

开发者 https://www.devze.com 2023-03-08 03:31 出处:网络
there is html structure: <a onclick=\"setString()\"> <img alt=\"no\" /> </a> I need to get attribute of a,that had been clicked,e.g. alt.

there is html structure:

<a onclick="setString()">
  <img alt="no" />
</a>

I need to get attribute of a,that had been clicked,e.g. alt.

How to know with setString() js function ,

image wi开发者_C百科th what alt attribute is clicked?

I assume,that somehow with this,but don't know how.


onclick="setString(this)

///Javascript Code:

function setString(element){

var value = element.children[0].alt;

}


I would change my HTML to remove the bits that aren't needed...

<img alt="no" onclick="setString(this.alt);" />

Rather than wrapping the image in an anchor tag, style it with CSS if you want...

cursor: pointer;
0

精彩评论

暂无评论...
验证码 换一张
取 消