Calling javaScript Function

ε祈祈猫儿з 提交于 2019-12-11 04:12:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!