JavaScript学习笔记(三)

来源:互联网 发布:java中list的用法 编辑:程序博客网 时间:2024/04/28 20:44

1.如何判断是不按下Ctrl键

 <BODY onmousedown="isKeyPressed(event)">  <p>鼠标点击</p>  <p>本例演示是否按下ctrl键</p> </BODY>

 <script type="text/javascript">function isKeyPressed(event){if(event.ctrlKey==1){alert("ctrl按下");}else{alert("ctrl没有按下");}}  </script>

2.通过表单提交,改变图片的边框植,高度和宽度

<BODY>  <img id="img1" src="ling2.jpg" alt="image图片" border="10" height="52" width="102">  <form name="form1">边框值:<input name="border" type="text" size="5"/>宽度:<input name="width" type="text" size="5"/>高度:<input name="height" type="text" size="5"/><input name="submit" type="button" value="提交" onClick="img()">  </form> </BODY>

  <script type="text/javascript">function img(){var image1 = document.getElementById("img1");image1.setAttribute("border",document.form1.border.value);image1.setAttribute("width",document.form1.width.value);image1.setAttribute("height",document.form1.height.value);}  </script>

3.点击鼠标将返回一个相对于屏幕的x和y坐标值

 <BODY onmousedown="screen(event)">  <p>点击鼠标</p>  <p>程序将返回一个相对于屏幕的X和Y坐标值</p> </BODY>

 <script type="text/javascript">function screen(event){x = event.screenX;y = event.screenY;alert("X="+x+" Y="+y);}  </script>

4.获取数组中的最大值和最小值无需对数组进行排序

 <script type="text/javascript">var myarray = new Array(2,5,8,6,1,3);var max = Math.max.apply(null,myarray);var min = Math.min.apply(null,myarray);document.write("max="+max+" min="+min);  </script>
5.将表单提交的用户名和密码存储到Cookie中

 <form name="form1" method="post" onsubmit="check()">用户名:<input name="user" type="text"><br>密码:<input name="password" type="password"><br><input type="submit" name="submit" value="提交">  </form>

<script type="text/javascript">function check(){var time = new Date();time.setDate(time.getDate()+1);if(document.form1.user.value!=""&&document.form1.password.value!=""){document.cookie=encodeURI("user="+document.form1.user.value)+";expires="+time.toUTCString();document.cookie=encodeURI("pass="+document.form1.password.value)+";expires="+time.toUTCString();}else{alert("你没有输入用户名或者密码");}}  </script>

6.显示当前时间

 <BODY onLoad="showtime()">  <form name="clock"><p><input name="thetime" style="font-size:9pt;color:#000000;border:0" size="50"></p>  </form> </BODY>

<script type="text/javascript">function showtime(){var today = new Date();var day;var date;if(today.getDay()==0)day = "星期日";if(today.getDay()==1)day = "星期一";if(today.getDay()==2)day = "星期二";if(today.getDay()==3)day = "星期三";if(today.getDay()==4)day = "星期四";if(today.getDay()==5)day = "星期五";if(today.getDay()==6)day = "星期六";var hours = today.getHours();var minutes = today.getMinutes();var seconds = today.getSeconds();document.fgColor="000000";date = "今天是"+today.getYear()+"年"+(today.getMonth()+1)+"月"+today.getDate()+"日"+day+"";var timeValue = date+" "+(hours>=12?"下午 ":"上午 ");timeValue += hours>12?hours-12:hours;timeValue += (minutes<10?":0":":")+minutes;timeValue += (seconds<10?":0":":")+seconds;document.clock.thetime.value = timeValue;setTimeout("showtime()",1000);}  </script>



原创粉丝点击