Can I dynamically take a Html element onclick attribute and use it in javascript?
<input type="button" onclick="???" 开发者_高级运维id="1" value="Button1" />
Without knowing what the name of the function in the onclick attribute, can I take the function from the onclick attribute and call it in javascript? If so, how can I do that?
If you just want to trigger click event which will call the onclick function, you can do something like this, using jquery
$('#1').click()
If you're using jQuery:
eval($('#1').attr('onclick'));
This should work, NOTE: IDs are not allowed to start with a number:
<input type="button" onclick="???" id="Button1 value="Button1" />
document.getElementById("Button1").onclick();
Example
The way to do it in jQuery would be $('#Button1').trigger('click')
using trigger
精彩评论