方法1:将Java Script脚本语言直接插入到JSP中进行调用
测试代码:
< page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"gt; <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <!--该代码实现的是一个简单的动态时钟--> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>时钟</title> <style type="text/css"> </style> </head> <!-- JavaScript代码块 --> <script type="text/javascript"> function startTime() { var today = new Date() var h = today.getHours() var m = today.getMinutes() var s = today.getSeconds() // add a zero in front of numbers<10 m = checkTime(m) s = checkTime(s) document.getElementById('txt').innerHTML = h + ":" + m + ":" + s t = setTimeout('startTime()', 500) } function checkTime(i) { if (i < 10) { i = "0" + i } return i } </script> <!-- 调用 --> <body "startTime()"> <div id="txt"></div> </body> </body> </html>
方法2:通过οnclick=“function()” 的方法进行调用
文件目录如下:
注意:JS文件夹放置在WebContent目录下,不要放置在META_INF或者WEB-INF下(之前放置在WEB_INF,我说咋调用不动…)。
Time,.js的代码:
/* 获取系统时间 */ function startTime() { var today = new Date() var h = today.getHours() var m = today.getMinutes() var s = today.getSeconds() // add a zero in front of numbers<10 m = checkTime(m) s = checkTime(s) document.getElementById('txt').innerHTML = h + ":" + m + ":" + s t = setTimeout('startTime()', 500) } function checkTime(i) { if (i < 10) { i = "0" + i } return i }
index.jsp的代码:
< page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"gt; <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>时钟</title> <style type="text/css"> </style> </head> <!--加载脚本路径--> <script type="text/javascript" src="JS/Time.js"></script> <!--调用方法--> </head> <div id="txt"></div> <body "startTime()"> </body> </html>
更多前端开发知识,请查阅 HTML中文网 !!
以上就是jsp中怎么用javascript?的详细内容,更多请关注易知道|edz.cc其它相关文章!