how to trigger button onmouseup handler from javascript
how to trigger onmouseup handler from javascript
i hav开发者_如何学Pythone my button
<input id="x" onmouseup="doStuff()">
and i want to trigger
document.getElementById("x").onmouseup();?????
If you want to use event binding there is always this way of doing things
document.getElementById("x").addEventListener("mouseup", function(){
alert('triggered');
}, false);
Here is the example JSFiddle of it.
Or if you want to actually "Trigger the event" try this
var evt = document.createEvent("MouseEvents");
evt.initEvent("mouseup", true, true);
document.getElementById("x").dispatchEvent(evt);
You've already answered your question :)
Simply call
document.getElementById("x").onmouseup()
in your code.
I've added an example here:
http://jsfiddle.net/cUCWn/2/
How about binding the mouseup event of "x" to a function, and then call that function from doStuff?
You pretty much have the answer there.
<input id="x" onmouseup="doStuff()" />
and then just declare your function
<script type="javascript">
function doStuff() {
alert("Yay!");
}
</script>
Alternatively, you can attach the event from Javascript. i.e.
<script type="text/javascript">
var x = document.getElementById("x");
x.onmouseup = doSomething;
function doSomething() {
alert("Yay!");
}
</script>
精彩评论