JS常用方法
以下为web开发中常用到的js方法;收集并整理出来;简单的就不写出来了
// 返回字符的长度,一个中文算2个
String.prototype.ChineseLength=function()
{
return this.replace(/[^\x00-\xff]/g,"**").length;
}
// 判断字符串是否以指定的字符串结束
String.prototype.EndsWith = function(str)
{
return this.substr(this.length - str.length) == str;
}
// 去掉字符左端的的空白字符
String.prototype.LeftTrim = function()
{
return this.replace(/(^[\\s]*)/g, "");
}
// 去掉字符右端的空白字符
String.prototype.RightTrim = function()
{
return this.replace(/([\\s]*$)/g, "");
}
// 判断字符串是否以指定的字符串开始
String.prototype.StartsWith = function(str)
{
return this.substr(0, str.length) == str;
}
// 去掉字符两端的空白字符
String.prototype.Trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}
1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2
<HTML>
3
<HEAD>
4
<TITLE> New Document </TITLE>
5
<META NAME="Generator" CONTENT="EditPlus">
6
<META NAME="Author" CONTENT="">
7
<META NAME="Keywords" CONTENT="">
8
<META NAME="Description" CONTENT="">
9
<script language=javascript>
10
String.prototype.Replace = function(oldValue,newValue)
11
{
12
var reg = new RegExp(oldValue,"g");
13
return this.replace(reg, newValue);
14
}
15
//字符串替换;曾经很头疼写了很多代码,还是这个简单
16
function replace(obj)
17
{
18
alert(obj.value.Replace("a","d"));
19
}
20
21
// 另存为文件
22
function SaveCode(obj, filename)
23
{
24
var win = window.open('', '_blank', 'top=100');
25
var code = obj.innerText;
26
code = code == null || code == "" ? obj.value : code;
27
win.opener = null;
28
win.document.write(code);
29
win.document.execCommand('saveas', true, filename);
30
win.close();
31
}
32
// 问候
33
window.onload = function()
34
{
35
var now = new Date();
36
var hour = now.getHours();
37
var greeting;
38
if (hour < 6)
39
greeting = "凌晨好";
40
else if (hour < 10)
41
greeting = "早上好";
42
else if (hour < 14)
43
greeting = "中午好";
44
else if (hour < 18)
45
greeting = "下午好";
46
else
47
greeting = "晚上好";
48
49
document.getElementById("hi").innerHTML = "<font color=red>" + greeting + "</font>" ;
50
}
51
// 将光标停在对象的最后
52
function PutCursorAtLast(obj)
53
{
54
obj.focus();
55
var range = obj.createTextRange();
56
range.moveStart('character',obj.value.length);
57
range.collapse(true);
58
range.select();
59
}
60
// 将光标停在对象的最前
61
function PutCursorAtFirst(obj)
62
{
63
obj.focus();
64
var range = obj.createTextRange();
65
range.moveStart('character',0);
66
range.collapse(true);
67
range.select();
68
}
69
</script>
70
</HEAD>
71
72
<BODY>
73
<span id="hi"></span>
74
<br/>
75
<span> curssor at last </span>
76
<br/>
77
<input type=text value="curssor at last" onclick=PutCursorAtLast(this)>
78
<br/>
79
<span> curssor at first </span>
80
<br/>
81
<input type=text value="curssor at first" onclick=PutCursorAtFirst(this)>
82
<br/>
83
<span> String.Replace </span>
84
<br/>
85
<INPUT TYPE=TEXT value="replace" ONCLICK=replace(this)>
86
<br/>
87
<span> save file </span>
88
<br/>
89
<input type=text value="hello word" onclick=SaveCode(this,"save")>
90
</BODY>
91
</HTML>
92
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">2
<HTML>3
<HEAD>4
<TITLE> New Document </TITLE>5
<META NAME="Generator" CONTENT="EditPlus">6
<META NAME="Author" CONTENT="">7
<META NAME="Keywords" CONTENT="">8
<META NAME="Description" CONTENT="">9
<script language=javascript>10
String.prototype.Replace = function(oldValue,newValue) 11
{ 12
var reg = new RegExp(oldValue,"g"); 13
return this.replace(reg, newValue); 14
}15
//字符串替换;曾经很头疼写了很多代码,还是这个简单16
function replace(obj)17
{18
alert(obj.value.Replace("a","d"));19
}20

21
// 另存为文件22
function SaveCode(obj, filename) 23
{24
var win = window.open('', '_blank', 'top=100'); 25
var code = obj.innerText; 26
code = code == null || code == "" ? obj.value : code; 27
win.opener = null;28
win.document.write(code);29
win.document.execCommand('saveas', true, filename);30
win.close();31
}32
// 问候33
window.onload = function()34
{ 35
var now = new Date();36
var hour = now.getHours();37
var greeting;38
if (hour < 6)39
greeting = "凌晨好";40
else if (hour < 10)41
greeting = "早上好";42
else if (hour < 14)43
greeting = "中午好";44
else if (hour < 18)45
greeting = "下午好";46
else 47
greeting = "晚上好";48
49
document.getElementById("hi").innerHTML = "<font color=red>" + greeting + "</font>" ;50
}51
// 将光标停在对象的最后52
function PutCursorAtLast(obj) 53
{ 54
obj.focus();55
var range = obj.createTextRange(); 56
range.moveStart('character',obj.value.length); 57
range.collapse(true); 58
range.select(); 59
}60
// 将光标停在对象的最前61
function PutCursorAtFirst(obj) 62
{ 63
obj.focus();64
var range = obj.createTextRange(); 65
range.moveStart('character',0); 66
range.collapse(true); 67
range.select(); 68
}69
</script>70
</HEAD>71

72
<BODY>73
<span id="hi"></span>74
<br/>75
<span> curssor at last </span>76
<br/>77
<input type=text value="curssor at last" onclick=PutCursorAtLast(this)>78
<br/>79
<span> curssor at first </span>80
<br/>81
<input type=text value="curssor at first" onclick=PutCursorAtFirst(this)>82
<br/>83
<span> String.Replace </span>84
<br/>85
<INPUT TYPE=TEXT value="replace" ONCLICK=replace(this)>86
<br/>87
<span> save file </span>88
<br/>89
<input type=text value="hello word" onclick=SaveCode(this,"save")>90
</BODY>91
</HTML>92

// 返回字符的长度,一个中文算2个
String.prototype.ChineseLength=function()
{
return this.replace(/[^\x00-\xff]/g,"**").length;
}
// 判断字符串是否以指定的字符串结束
String.prototype.EndsWith = function(str)
{
return this.substr(this.length - str.length) == str;
}
// 去掉字符左端的的空白字符
String.prototype.LeftTrim = function()
{
return this.replace(/(^[\\s]*)/g, "");
}
// 去掉字符右端的空白字符
String.prototype.RightTrim = function()
{
return this.replace(/([\\s]*$)/g, "");
}
// 判断字符串是否以指定的字符串开始
String.prototype.StartsWith = function(str)
{
return this.substr(0, str.length) == str;
}
// 去掉字符两端的空白字符
String.prototype.Trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}1
//给定一个时间返回该时间是当前月的第几周
2
function getWeek(a, b, c) {
3
var date = new Date(a, b - 1, c);
4
return Math.ceil((c + 7 - (date.getDay() || 7)) / 7);
5
}
//给定一个时间返回该时间是当前月的第几周2
function getWeek(a, b, c) {3
var date = new Date(a, b - 1, c);4
return Math.ceil((c + 7 - (date.getDay() || 7)) / 7);5
}
TrackBack:http://www.cnblogs.com/doll-net/archive/2007/03/13/673183.html




浙公网安备 33010602011771号