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

我有以下两个javascript函数:


1个


showCountry()

2


showUser()

我想将它们放在外部“ .js”文件中


1个


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

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>

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


POPMUISE
浏览 596回答 3
3回答

有只小跳蛙

像这样的代码&nbsp;<html>&nbsp; &nbsp; <head>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <script type="text/javascript" src="path/to/script.js"></script>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!--other script and also external css included over here-->&nbsp; &nbsp; </head>&nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp; <form>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <select name="users" onChange="showUser(this.value)">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<option value="1">Tom</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<option value="2">Bob</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<option value="3">Joe</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </select>&nbsp; &nbsp; &nbsp; &nbsp; </form>&nbsp; &nbsp; </body>&nbsp; &nbsp; </html>希望对您有帮助。...谢谢

Cats萌萌

注意:-不要在外部JavaScript文件中使用脚本标签。<html><head></head><body>&nbsp; &nbsp; <p id="cn"> Click on the button to change the light button</p>&nbsp; &nbsp; <button type="button" onclick="changefont()">Click</button>&nbsp; &nbsp; &nbsp;<script src="external.js"></script></body>外部Java脚本文件:-&nbsp; &nbsp; &nbsp; &nbsp; function changefont()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var x = document.getElementById("cn");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x.style.fontSize = "25px";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x.style.color = "red";&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }

拉风的咖菲猫

这是将外部javascript文件包含到HTML标记中的方法。<script type="text/javascript" src="/js/external-javascript.js"></script>external-javascript.js外部文件要包含在哪里。包含路径和文件名时,请确保其正确。<a href="javascript:showCountry('countryCode')">countryCode</a>上面提到的方法对于锚标记是正确的,并且可以完美地工作。但是对于其他元素,您应该明确指定事件。例:<select name="users" onChange="showUser(this.value)">谢谢,XmindZ
打开App,查看更多内容
随时随地看视频慕课网APP