开发者

Javascript: get element's current "onclick" contents

开发者 https://www.devze.com 2023-01-07 01:30 出处:网络
I have a page with the following code: <a id=\"test\" href=\"someurl\" onclick=\"somefunction\">link</a>

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 undefi开发者_如何学JAVAned. What do I need to do to get somefunction?


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

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


You should try:

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


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>
0

精彩评论

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