编写javaScript代码实现用户登录时数据合法性校验功能,界面如图教材P338 第2题,效果如下图所示: 图1 显示列表项内容 二、日期的处理 实时显示当前时间及累计登录时间,如下图2所示。[提示window.setInterval(function,1000),onload事件]。 图2时间处理 三、实现电话拨号键盘 如图3所示,点击数字文本框中会依次显示拨打的号码。 图3拨号键盘
|
1 、<script> function $(id) { return document.getElementById(id); } function sum() { var index =$("a").selectedIndex; var x=$("a").options[index].value; var z=$("a").options[index].text; alert("教材名称:"+z+"\n"+"定价:"+x); } </script> <select name="" id="a" size="5" onChange="sum()">
<script> var now1=new Date(); var y2=now1.getFullYear(); var m2=now1.getMonth()+1; var d2=now1.getDate(); var h2=now1.getHours(); var mi2=now1.getMinutes(); var s2=now1.getSeconds(); function time() { var now=new Date();
var y=now.getFullYear(); var m=now.getMonth()+1; var d=now.getDate(); var h=now.getHours(); var mi=now.getMinutes(); var s=now.getSeconds(); document.getElementById("time").innerHTML = "当前时间:"+y + "年" + m + "月" + d + "日" + "\t" + h + ":" + mi +":" + s; var y1=y-y2; var m1=m-m2; var d1=d-d2; var h1=h-h2; var mi1=mi-mi2; var s1=s-s2; document.getElementById("time1").innerHTML = "您已登录,累计时间:"+y1 + "年" + m1 + "月" + d1 + "日" + "\t" + h1 + ":" + mi1 +":" + s1; } setInterval("time()",1000); </script> 借助js本身获取Date()时间函数再结合取时间的c值最后加上调用id就可以完成功能。 3. <script> function sum(num) { document.getElementById("text1").value+=num; } </script> <form action=""> <input type="text" id="text1"><br><br> 借助<script>函数加上id的调用就可以完成功能。 |
1. 1 2. 2 3. 3 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script> function $(id) { return document.getElementById(id); } function sum() { var index =$("a").selectedIndex; var x=$("a").options[index].value; var z=$("a").options[index].text; alert("教材名称:"+z+"\n"+"定价:"+x); } </script> </head> <body> <h3>显示列表的内容</h3> <form action=""> <select name="" id="a" size="5" onChange="sum()"> <option value="35元">计算机组成原理</option> <option value="38元" >数据结构</option> <option value="43元">计算机网络</option> <option value="40元">java程序设计</option> <option value="28元">算法分析</option> </select> </form> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script> var now1=new Date(); var y2=now1.getFullYear(); var m2=now1.getMonth()+1; var d2=now1.getDate(); var h2=now1.getHours(); var mi2=now1.getMinutes(); var s2=now1.getSeconds(); function time() { var now=new Date();
var y=now.getFullYear(); var m=now.getMonth()+1; var d=now.getDate(); var h=now.getHours(); var mi=now.getMinutes(); var s=now.getSeconds(); document.getElementById("time").innerHTML = "当前时间:"+y + "年" + m + "月" + d + "日" + "\t" + h + ":" + mi +":" + s; var y1=y-y2; var m1=m-m2; var d1=d-d2; var h1=h-h2; var mi1=mi-mi2; var s1=s-s2; document.getElementById("time1").innerHTML = "您已登录,累计时间:"+y1 + "年" + m1 + "月" + d1 + "日" + "\t" + h1 + ":" + mi1 +":" + s1; } setInterval("time()",1000); </script> </head> <body> <h2 style="text-align: center;">date的处理</h2> <hr> <h3 id="time" style="text-align: center;"></h3> <h3 id="time1" style="text-align: center;"></h3> </body> </html> 3. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script> function sum(num) { document.getElementById("text1").value+=num; } </script> </head> <body> <h2>实现电话拨号键盘</h2> <hr> <form action=""> <input type="text" id="text1"><br><br> <input type="button" onclick="sum('1')" value="1" > <input type="button" onclick="sum('2')" value="2" > <input type="button" onclick="sum('3')" value="3" ><br> <input type="button" onclick="sum('4')" value="4" > <input type="button" onclick="sum('5')" value="5" > <input type="button" onclick="sum('6')" value="6" ><br> <input type="button" onclick="sum('7')" value="7" > <input type="button" onclick="sum('8')" value="8" > <input type="button" onclick="sum('9')" value="9" ><br> <input type="button" onclick="sum('*')" value="*" > <input type="button" onclick="sum('0')" value="0" > <input type="button" onclick="sum('#')" value="#" > </form> </body> </html> |