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

2020-02-10 12:52发布

问题:

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?

回答1:

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.



回答2:

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>


回答3:

You should try:

document.getElementById('test').onclick.toString()