问题
I am adding my external .js file as
<head>
<script type="text/javascript" src="service/js/test.js"></script>
</head>
Inside the test.js
I have one function called functionTest()
.
Calling this function onload of the body is working fine;
<body onload="functionTest()">
</body>
My Question is:
I want to call this function two time in the two different divs. How should I achieve this ?
<body>
<div id="left">
<!-- Directly Want to call functionTest() when div is getting loaded -->
</div>
<div id="right">
<!-- Directly Want to call functionTest() when div is getting loaded -->
</div>
</body>
回答1:
Add the call to the function at the end of the div
. The function functionTest
will be called when the div
is completely loaded.
<body>
<div id="left">
<!-- Directly Want to call functionTest() when div is getting loaded -->
<script>functionTest();</script>
<!-- ^^^^^^^^^^^^^^^ -->
</div>
<div id="right">
<!-- Directly Want to call functionTest() when div is getting loaded -->
<script>functionTest();</script>
<!-- ^^^^^^^^^^^^^^^ -->
</div>
</body>
回答2:
Just insert <script> functionTest() </script>
inside each div
来源:https://stackoverflow.com/questions/31019225/calling-javascript-function