Get attribute of clicked link with JS function

天涯浪子 提交于 2021-02-11 13:49:21

问题


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 with what alt attribute is clicked?

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


回答1:


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;



回答2:


onclick="setString(this)

///Javascript Code:

function setString(element){

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

}


来源:https://stackoverflow.com/questions/6126923/get-attribute-of-clicked-link-with-js-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!