How to run more than one JavaScript function with one click
How do I run five JavaScript function开发者_如何学JAVAs in one click?
Sample code:
<img alt="move right"
src="Pictures/sides/right.gif"
id="MoveRight"
***onclick="javascript:moveObjRight('ground','sky','mountain');"***
style="z-index: 1; left: 801px; top: 37px; position: absolute; height: 33px; width: 34px;"
/>
<img alt="move right" src="Pictures/sides/right.gif" id="MoveRight"
onclick="moveObjRight('ground','sky','mountain');myFunction2();myFunction3();myFunction4();myFunction5();"
style="z-index: 1; left: 801px; top: 37px; position: absolute; height: 33px;
width: 34px;" />
or
<script type="text/javascript">
window.onload = function() {
var ele = document.getElementById('MoveRight');
ele.onclick = function() {
moveObjRight('ground','sky','mountain');
myFunction2();
myFunction3();
myFunction4();
myFunction5();
}
};
</script>
Note that the second option will override any previously assigned window.onload
and ele.onclick
function. If you are using a javascript library use the appropriate dom ready event (e.g. jQuery: $(document).ready(function(){/*code*/});
or short: $(function(){/*code*/})
)
You can add all your functions in the onclick property of the img tag
<img onclick="fnc1();fnc2();..." />
You can call a single method on your on click event and in that method you can call all your other methods.
Call multiple functions in an other function
<script type="text/javascript">
function CallMultipleFunctions() {
Function1();
Function2();
Function3();
Function4();
}
</script>
<img onclick= "CallMultipleFunctions()"/>
EDIT:
Check this link
精彩评论