JavaScript 实用方法

1.按时间显示问候语

2.强制光标停留位置

3.保存页面文本

 1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
 2 <html><head>
 3     <title>JS常用方法</title>
 4     <meta name="Generator" content="EditPlus">
 5     <meta name="Author" content="">
 6     <meta name="Keywords" content="">
 7     <meta name="Description" content="">
 8     <script language="JavaScript">
 9 
10     <!-- 另存为文件-->
11     function SaveCode(obj, filename) 
12     {
13         var win = window.open('', '_blank', 'top=100'); 
14         var code = obj.innerText; 
15         code = code == null || code == "" ? obj.value : code; 
16         win.opener = null;
17         win.document.write(code);
18         win.document.execCommand('saveas', true, filename);
19         win.close();
20     }
21     <!-- 问候-->
22     window.onload = function()
23     {    
24         var now = new Date();
25         var hour = now.getHours();
26         var greeting;
27         if (hour < 6)
28             greeting = "凌晨好";
29         else if (hour < 10)
30             greeting = "早上好";
31         else if (hour < 14)
32             greeting = "中午好";
33         else if (hour < 18)
34             greeting = "下午好";
35         else 
36             greeting = "晚上好";
37             
38         document.getElementById("hi").innerHTML = "<font color=red>" + greeting + "</font>" ;
39     }
40     <!-- 将光标停在对象的最后-->
41     function PutCursorAtLast(obj) 
42     {  
43         obj.focus();
44         var range = obj.createTextRange(); 
45         range.moveStart('character',obj.value.length); 
46         range.collapse(true); 
47         range.select(); 
48     }
49     <!-- 将光标停在对象的最前-->
50     function PutCursorAtFirst(obj) 
51     {  
52         obj.focus();
53         var range = obj.createTextRange(); 
54         range.moveStart('character',0); 
55         range.collapse(true); 
56         range.select(); 
57     }
58 </script>
59 </head>
60 <body>
61 
62 <span id="hi"></span><br />
63 
64 <span>curssor at last </span><br />
65 <input type="text" value="curssor at last" onclick="PutCursorAtLast(this)">
66 <br />
67 
68 <span>curssor at first </span><br />
69 <input type="text" value="curssor at first" onclick="PutCursorAtFirst(this)">
70 <br />
71 
72 <span>save file </span><br />
73 <input type="text" value="hello word" onclick='SaveCode(this,"save")' /><br/>
74 
75 </body>
76 </html>

 

posted @ 2016-06-21 11:53  何鸿涛  阅读(196)  评论(0编辑  收藏  举报