js类库大全
/*------------------------------------------------------------
7 *-----------------------------------------------------------*/
8
9
10 /**//* $ 获取指定对象
11 @element 对象名
12 可以使用对象名集合,返回值为对象的集合
13 如果您使用了 Prototype 类库, 请把该函数注释掉
14 Sams_object.Get() 中同样实现该函数的所有功能
15 */
16 function $(element) {
17 if (arguments.length > 1) {
18 for (var i = 0, elements = [], length = arguments.length; i < length; i++)
19 elements.push($(arguments[i]));
20 return elements;
21 }
22 if (typeof element == 'string')
23 element = document.getElementById(element);
24 return element;
25 }
26
27 /// 浏览器相关操作
28 var Sams_browse = {
29 /**//* 检测浏览信息 */
30 checkBrowser : function ()
31 {
32 this.ver=navigator.appVersion
33 this.dom=document.getElementById?1:0
34 this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom)?1:0;
35 this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom)?1:0;
36 this.ie4=(document.all && !this.dom)?1:0;
37 this.ns5=(this.dom && parseInt(this.ver) >= 5) ?1:0;
38 this.ns4=(document.layers && !this.dom)?1:0;
39 this.mac=(this.ver.indexOf('Mac') > -1) ?1:0;
40 this.ope=(navigator.userAgent.indexOf('Opera')>-1);
41 this.ie=(this.ie6 || this.ie5 || this.ie4)
42 this.ns=(this.ns4 || this.ns5)
43 this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns5 || this.ns4 || this.mac || this.ope)
44 this.nbw=(!this.bw)
45 return this;
46 },
47
48 /**//* 设为首页
49 @url 要设为首页的地址
50 */
51 SetDefault : function ()
52 {
53 this.style.behavior='url(#default#homepage)';
54 this.setHomePage(this.GetUrl());
55 return false;
56 },
57
58 /**//* 复制指定URL地址
59 @Msg 要写入剪贴板的字符集
60 */
61 SetCopy : function (Msg){
62 if(navigator.userAgent.toLowerCase().indexOf('ie') > -1) {
63 clipboardData.setData('Text',Msg);
64 alert ("网址“"+Msg+"”\n已经复制到您的剪贴板中\n您可以使用Ctrl+V快捷键粘贴到需要的地方");
65 }
66 else
67 {
68 prompt("请复制网站地址:",Msg);
69 }
70 },
71
72 /**//* 加入收藏
73 @site 站点名称
74 @url 地址
75 */
76 AddBookmark : function (site, url){
77 if(navigator.userAgent.toLowerCase().indexOf('ie') > -1) {
78 window.external.addFavorite(url,site)
79 } else if (navigator.userAgent.toLowerCase().indexOf('opera') > -1) {
80 alert ("请使用Ctrl+T将本页加入收藏夹");
81 } else {
82 alert ("请使用Ctrl+D将本页加入收藏夹");
83 }
84 },
85
86 /**//* 打开Url指定宽度和高度的窗口 */
87 OpenWindows : function (url,width,height)
88 {
89 window.open(url,'newwin','width='+width+',height='+height);
90 return false;
91 },
92
93 /**//* 禁止浏览器的Javascript错误提示 */
94 CloseError : function(){
95 window.onerror = function(){return true;};
96 },
97
98 /**//* 获取浏览器URL */
99 GetUrl : function(){
100 return location.href;
101 },
102
103 /**//* 获取URL参数 */
104 GetUrlParam : function(){
105 return location.search;
106 },
107
108 /**//* 获取页面来源 */
109 GetFrom : function(){
110 return document.referrer;
111 },
112
113 /**//* 获取指定的URL参数值
114 @name 参数名
115 */
116 Request : function(name){
117 var GetUrl = this.GetUrl();
118 var Plist = new Array();
119 if(GetUrl.indexOf('?') > 0)
120 {
121 Plist = GetUrl.split('?')[1].split('&');
122 }
123 else if(GetUrl.indexOf('#') > 0)
124 {
125 Plist = GetUrl.split('#')[1].split('&');
126 }
127 if (GetUrl.length > 0)
128 {
129 for(var i=0; i<Plist.length; i++)
130 {
131 var GetValue = Plist[i].split('=');
132 if (GetValue[0].toUpperCase() == name.toUpperCase())
133 {
134 return GetValue[1];
135 break;
136 }
137 }
138 return;
139 }
140 },
141
142 /**//* 直接将HTML写到新窗口
143 @title 标题
144 @msg 内容
145 */
146 Popmsg : function PopIt(title,msg)
147 {
148 var popup = window.open('','popDialog','height=500,width=400,scrollbars=yes');
149 popup.document.write('<html><title>'+title+'</title><style>body{margin:10px;font:13px Arial;}span{text- line:20px;}</style><body><span style=\'font:14px arial;\'>'+msg + '</span></body></html>');
150 popup.document.close();
151 }
152 };
153
154
155 /// 对象操作
156 var Sams_object = {
157
158 /**//* 创建一个DIV对象
159 @ID 要创建的对象ID
160 @ClassName 创建对象的Class
161 @SetValue 设置该对象值
162 @ToDiv 将对象追加到指定的对象,如指定的对象不存在,则追加在Body的后面
163 返回 创建后的对象
164 */
165 CreateDiv : function (ID,ClassName,SetValue,ToDiv){
166 var creatediv = document.createElement('div');
167 if(ID != null) creatediv.id = ID;
168 creatediv.style.position = 'absolute';
169 if(ClassName != null) creatediv.className = ClassName;
170 if(this.Get(ToDiv))
171 {
172 this.Get(ToDiv).appendChild(creatediv);
173 }
174 else
175 {
176 document.getElementsByTagName('body')[0].appendChild(creatediv);
177 }
178 this.SetValue(ID,SetValue);
179 return this.Get(ID);
180 },
181
182 /**//* 删除指定DIV对象
183 @objid 要删除的对象ID
184 返回 Bool 操作结果
185 */
186 DeleteDiv : function (objid)
187 {
188 try
189 {
190 if(this.Get(objid))
191 {
192 var GetParent = this.Get(objid).parentNode;
193 GetParent.removeChild(this.Get(objid));
194 return true;
195 }
196 else
197 {
198 return false;
199 }
200 }
201 catch(e)
202 {
203 return false;
204 }
205 },
206
207 /**//* 获取浏览器对象
208 @id 要获取的对象ID
209 可以使用对象名集合,返回值为对象的集合
210 */
211 Get : function (objid) {
212 if (arguments.length > 1) {
213 for (var i = 0, objids = [], length = arguments.length; i < length; i++)
214 objids.push(this.Get(arguments[i]));
215 return objids;
216 }
217 if (typeof objid == 'string')
218 {
219 if (document.getElementById) {
220 objid = document.getElementById(objid);
221 } else if (document.all) {
222 objid = document.all[objid];
223 } else if (document.layers) {
224 objid = document.layers[objid];
225 }
226 }
227 return objid;
228 },
229
230 /**//* 获取对象的值
231 @objid 对象ID
232 */
233 GetValue : function (objid) {
234 if (typeof objid == 'string')
235 {
236 var getTagName = this.Get(objid).tagName.toLowerCase();
237 if (getTagName == 'input' || getTagName == 'textarea' || getTagName == 'select')
238 {
239 return this.Get(objid).value;
240 }
241 else if (getTagName == 'div' || getTagName == 'span')
242 {
243 return this.Get(objid).innerText;
244 }
245 }
246 else if (typeof objid == 'object')
247 {
248 return objid.value;
249 }
250 },
251