常用的一些自定义函数
1 function $id()
2 {
3 var obj,name;
4 if( arguments.length==2 )
5 {
6 obj = arguments[0];
7 name = arguments[1];
8 }else if( arguments.length==1 ){
9 obj = document;
10 name = arguments[0];
11 }else{
12 return alert( "$id have error!" );
13 }
14 return obj.getElementById(name);
15 }
16
17 function $selectedOption(obj,selectedValue)
18 {
19 for( var i=0;i<obj.options.length;i++ )
20 {
21 if( obj.options[i].value == selectedValue )
22 {
23 obj.options[i].setAttribute("selected","true");
obj.selectedIndex = i; //这行代码是后来加上去的,原因在IE中找不到selectedIndex
24 return;
25 }
26 }
27 }
28
29 function $name()
30 {
31 var obj,name;
32 if( arguments.length==2 )
33 {
34 obj = arguments[0];
35 name = arguments[1];
return obj[name]!==undefined ? obj[name]:obj.getElementsByName(name);
36 }else if( arguments.length==1 ){
37 obj = document;
38 name = arguments[0];
return obj.getElementsByName(name);
39 }else{
40 return alert( "$name have error!" );
41 }
43 }
44
45 function $tag()
46 {
47 var obj,name;
48 if( arguments.length==2 )
49 {
50 obj = arguments[0];
51 name = arguments[1];
52 }else if( arguments.length==1 ){
53 obj = document;
54 name = arguments[0];
55 }else{
56 return alert( "$tag have error!" );
57 }
58 return obj.getElementsByTagName(name);
59 }
60
61 String.prototype.trim = function() {
62 var str = this,
63 str = str.replace(/^\\s\\s*/, ''),
64 ws = /\\s/,
65 i = str.length;
66 while (ws.test(str.charAt(--i)));
67 return str.slice(0, i + 1);
68 }
2 {
3 var obj,name;
4 if( arguments.length==2 )
5 {
6 obj = arguments[0];
7 name = arguments[1];
8 }else if( arguments.length==1 ){
9 obj = document;
10 name = arguments[0];
11 }else{
12 return alert( "$id have error!" );
13 }
14 return obj.getElementById(name);
15 }
16
17 function $selectedOption(obj,selectedValue)
18 {
19 for( var i=0;i<obj.options.length;i++ )
20 {
21 if( obj.options[i].value == selectedValue )
22 {
23 obj.options[i].setAttribute("selected","true");
obj.selectedIndex = i; //这行代码是后来加上去的,原因在IE中找不到selectedIndex
24 return;
25 }
26 }
27 }
28
29 function $name()
30 {
31 var obj,name;
32 if( arguments.length==2 )
33 {
34 obj = arguments[0];
35 name = arguments[1];
return obj[name]!==undefined ? obj[name]:obj.getElementsByName(name);
36 }else if( arguments.length==1 ){
37 obj = document;
38 name = arguments[0];
return obj.getElementsByName(name);
39 }else{
40 return alert( "$name have error!" );
41 }
43 }
44
45 function $tag()
46 {
47 var obj,name;
48 if( arguments.length==2 )
49 {
50 obj = arguments[0];
51 name = arguments[1];
52 }else if( arguments.length==1 ){
53 obj = document;
54 name = arguments[0];
55 }else{
56 return alert( "$tag have error!" );
57 }
58 return obj.getElementsByTagName(name);
59 }
60
61 String.prototype.trim = function() {
62 var str = this,
63 str = str.replace(/^\\s\\s*/, ''),
64 ws = /\\s/,
65 i = str.length;
66 while (ws.test(str.charAt(--i)));
67 return str.slice(0, i + 1);
68 }