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

时间:2021-10-22 23:15:19

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?

调用这些函数的正确语法是什么?

6 个解决方案

#1


46  

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

我希望它会对你有所帮助....谢谢

#2


18  

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"; 
            }

#3


4  

In your head element add

在你的头元素添加

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

#4


3  

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. Just write the code and enjoy!

myScript将是JavaScript的文件名。只需编写代码即可享受!

#5


2  

This is the way to include an external javascript file to you HTML markup.

这是将HTML标记包含在外部javascript文件中的方法。

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

Where external-javascript.js is the external file to be included. Make sure the path and the file name are correct while you including it.

其中external-javascript.js是要包含的外部文件。包含它时,请确保路径和文件名正确无误。

<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

#6


0  

I hope this helps someone here: I encountered an issue where I needed to use JavaScript to manipulate some dynamically generated elements. 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. 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.

我希望这可以帮助这里的人:我遇到了一个问题,我需要使用JavaScript来操作一些动态生成的元素。将代码包含到我在头部的

#1


46  

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

我希望它会对你有所帮助....谢谢

#2


18  

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"; 
            }

#3


4  

In your head element add

在你的头元素添加

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

#4


3  

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. Just write the code and enjoy!

myScript将是JavaScript的文件名。只需编写代码即可享受!

#5


2  

This is the way to include an external javascript file to you HTML markup.

这是将HTML标记包含在外部javascript文件中的方法。

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

Where external-javascript.js is the external file to be included. Make sure the path and the file name are correct while you including it.

其中external-javascript.js是要包含的外部文件。包含它时,请确保路径和文件名正确无误。

<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

#6


0  

I hope this helps someone here: I encountered an issue where I needed to use JavaScript to manipulate some dynamically generated elements. 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. 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.

我希望这可以帮助这里的人:我遇到了一个问题,我需要使用JavaScript来操作一些动态生成的元素。将代码包含到我在头部的