简体   繁体   English

如何使用外部“.js”文件

[英]How to use external “.js” files

I have the following two javascript functions: 我有以下两个javascript函数:

1 1

showCountry()

2 2

showUser()

I would like to put them in external ".js" files 我想把它们放在外部的“.js”文件中

1 1

<a href="javascript:showCountry('countryCode')">countryCode</a>

2 2

<form>
 <select name="users" onChange="showUser(this.value)">
 <option value="1">Tom</option>
 <option value="2">Bob</option>
 <option value="3">Joe</option>
 </select>
</form>

What is the correct syntax to call these functions? 调用这些函数的正确语法是什么?

Code like this 像这样的代码

 <html>
    <head>
          <script type="text/javascript" src="path/to/script.js"></script>
          <!--other script and also external css included over here-->
    </head>
    <body>
        <form>
            <select name="users" onChange="showUser(this.value)">
               <option value="1">Tom</option>
               <option value="2">Bob</option>
               <option value="3">Joe</option>
            </select>
        </form>
    </body>
    </html>

I hope it will help you.... thanks 我希望它会对你有所帮助....谢谢

Note :- Do not use script tag in external JavaScript file. 注意: - 不要在外部JavaScript文件中使用脚本标记

<html>
<head>

</head>
<body>
    <p id="cn"> Click on the button to change the light button</p>
    <button type="button" onclick="changefont()">Click</button>

     <script src="external.js"></script>
</body>

External Java Script file:- 外部Java脚本文件: -

        function changefont()
            {

                var x = document.getElementById("cn");
                x.style.fontSize = "25px";           
                x.style.color = "red"; 
            }

在你的头元素添加

<script type="text/javascript" src="myscript.js"></script>

This is the way to include an external javascript file to you HTML markup. 这是向您添加外部JavaScript文件HTML标记的方法。

<script type="text/javascript" src="/js/external-javascript.js"></script>

Where external-javascript.js is the external file to be included. 其中external-javascript.js是要包含的外部文件。 Make sure the path and the file name are correct while you including it. 包含它时,请确保路径和文件名正确无误。

<a href="javascript:showCountry('countryCode')">countryCode</a>

The above mentioned method is correct for anchor tags and will work perfectly. 上述方法对于锚标签是正确的,并且将完美地工作。 But for other elements you should specify the event explicitly. 但是对于其他元素,您应该明确指定事件。

Example: 例:

<select name="users" onChange="showUser(this.value)">

Thanks, XmindZ 谢谢,XmindZ

You can simply add your JavaScript in body segment like this: 您可以在主体细分中添加JavaScript,如下所示:

<body>

<script src="myScript.js"> </script>
</body>

myScript will be the file name for your JavaScript. myScript将是JavaScript的文件名。 Just write the code and enjoy! 只需编写代码即可享受!

I hope this helps someone here: I encountered an issue where I needed to use JavaScript to manipulate some dynamically generated elements. 我希望这可以帮助这里的人:我遇到了一个问题,我需要使用JavaScript来操作一些动态生成的元素。 After including the code to my external .js file which I had referenced to between the <script> </script> tags at the head section and it was working perfectly, nothing worked again from the script.Tried using developer tool on FF and it returned null value for the variable holding the new element. 将代码包含到外部.js文件中后,我在头部的<script> </script>标记之间引用了它并且它工作正常, </script>没有任何工作。使用FF上的开发人员工具和它为包含新元素的变量返回null值。 I decided to move my script tag to the bottom of the html file just before the </body> tag and bingo every part of the script started to respond fine again. 我决定将我的脚本标记移动到</body>标记之前的html文件的底部,宾果游戏的每个部分都开始再次响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM