一、网页调用javascript的几种形式:

共有5种形式,分别是:头部嵌入, html嵌入,元素嵌入,调用外部JS文件,伪URL。

1.javascript嵌入网页头head中,示例:

<html>
<head><title>javascript嵌入网页头head中案例</title>
<script type="text/javascript">
  document.write("hello world!");
</script>
</head>
<body>
</
body> </html>

 

 

2.在html中嵌入javascript代码,示例:

<html>
<head><title>网页html中嵌入javascript案例</title>
</head>
<body>
<script type="text/javascript">
  document.write("hello world!");
</script>
</body>
</html>

 

3.在网页元素事件中嵌入javascript代码,示例:

<html>
<head><title>网页元素中嵌入javascript案例</title>
<script language="text/javascript">
     function validata(){
         window.alert("请输入内容");
    }
</script>
</head>
<body>
<form method="post" action="#">
<input type="text" name="txtname">
<input type="button" value="确定" onclick="validate()">
</body>
</html>

4.调用已存在的javascript文件,示例:

<html>
<head><title>网页中调用已有的javascript文件</title>
<script src="hello.js"></script>
</head>
<body>
</body>
</html>

5.通过javascript伪URL引入javascript代码,示例:

<html>
<head><title>伪URL地址引入javascript代码案例</title>
</head>
<body>
<form method="post" action="#">
<input type="text" name="txtname">
<input type="button" value="确定" onclick="javascript:alert(‘鼠标点击了!’)">
</body>
</html>

 

  

版权声明:本文为lingen163原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/lingen163/p/10961424.html