[removed] get element's current “onclick” cont

2020-02-10 12:56发布

I have a page with the following code:

<a id="test" href="someurl" onclick="somefunction">link</a>

I need to actually read the 'onclick' data. As such, I would like something to the effect of

alert(document.getElementById('test').onClick)

Sadly, it returns undefined. What do I need to do to get somefunction?

3条回答
ら.Afraid
2楼-- · 2020-02-10 12:57

Assuming the tag is well-formed, a tag's attribute can be obtained via Element.getAttribute.

document.getElementById('test').getAttribute('onclick')
// -> "somefunction"

Hopefully you aren't using onclick for some unscrupulous purpose like storing data.

查看更多
爷、活的狠高调
3楼-- · 2020-02-10 13:07

You should try:

document.getElementById('test').onclick.toString()
查看更多
4楼-- · 2020-02-10 13:15

You can use Javascript on anchor tag like:

<script type="text/javascript">

  function testClick(id)
  {
     alert(id);
  }

</script>

<a href="#" id="test" onclick="testclick(this.id);">Click</a>
查看更多
登录 后发表回答