随笔分类 -  js/jq

摘要:1 Function.prototype.method = function(name, func) { 2 if (!this.prototype[name]) { 3 this.prototype[name] = func; 4 } 5 }; 6 7 //根据数字的正负来判断使用哪个方法 8 Number.method('integer', function() { 9 return Math[this < 0 ? 'ceil' : 'floor'](this);10 });11 document.writeln((-10 / 3).i 阅读全文
posted @ 2012-07-09 13:07 小猩猩君 阅读(158) 评论(0) 推荐(0) 编辑
摘要:1 function newCydz(){2 window.showModalDialog('open_cydz.html','','dialogWidth=420px;dialogHeight=350px;scroll=no;status=no;help=no;');3 }效果和用JS写的Dialog一样,但是是一个新开窗口页面,展现效果可能木有JS写的好看,但是在低版本浏览器上性能是木有问题的。由于showModalDialog缓存严重,下面是在子窗口取消客户端缓存的设置.也可以在服务器端取消缓存1 <meta http-equiv=& 阅读全文
posted @ 2012-07-05 10:37 小猩猩君 阅读(201) 评论(0) 推荐(0) 编辑
摘要:1 var addHandler = document.body.addEventListener ? 2 function(target, eventType, handler) { 3 target.addEventListener(eventType, handler, false); 4 } : function(target, eventType, handler) { 5 target.attachEvent("on" + eventType, handler); 6 }; 7 8 var removeHandler = document.body.remove 阅读全文
posted @ 2012-07-02 17:07 小猩猩君 阅读(418) 评论(0) 推荐(0) 编辑
摘要:1 function addHandler(target, eventType, handler) { 2 if (target.addEventListener) { 3 addHandler = function(target, eventType, handler) { 4 target.addEventListener(eventType, handler, false); 5 }; 6 } else { 7 addHandler = function(target, eventType, handler) { 8 target.attachEvent("on" + 阅读全文
posted @ 2012-07-02 17:01 小猩猩君 阅读(228) 评论(0) 推荐(0) 编辑
摘要:等项目上线,写点简单的东西练练手 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> 3 <head> 4 <meta http-equiv="Cont 阅读全文
posted @ 2012-06-29 15:17 小猩猩君 阅读(406) 评论(0) 推荐(0) 编辑
摘要:简单的例子:1 for (var i = 0, len = item.length; i < len; i++) {2 process(item[i]);3 }优化条件:1、处理过程是否必须同步?2、数据是否必须按顺序处理?都是否,就看下面的代码: 1 function processArray(items, process, callback) { 2 var todo = items.concat(); 3 4 setTimeout(function() { 5 process(todo.shift()); 6 7 if (todo.length > 0) { 8 setTim 阅读全文
posted @ 2012-06-28 13:45 小猩猩君 阅读(841) 评论(0) 推荐(0) 编辑
摘要:1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> 3 <head> 4 <meta http-equiv="Content-Type" co 阅读全文
posted @ 2012-06-27 15:09 小猩猩君 阅读(660) 评论(0) 推荐(0) 编辑
摘要:1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN""http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> 3 <head> 4 <meta http-equiv="content-type" content="text/html; ch 阅读全文
posted @ 2012-06-27 10:51 小猩猩君 阅读(139) 评论(0) 推荐(0) 编辑
摘要:1 if (!String.prototype.trim) {2 String.prototype.trim = function() {3 return this.replace(/^\s+/, "").replace(/\s+$/, "");4 };5 }67 var str = " \t\n test string ".trim();8 console.log(str == "test string");//true混合解决方案:用正则表达式方法过滤头部空白,用非正则表达式的方法过滤尾部字符。 1 Strin 阅读全文
posted @ 2012-06-26 13:52 小猩猩君 阅读(2095) 评论(0) 推荐(0) 编辑
摘要:1 var str = "I'm a thirty-five character string.", 2 strs = [], 3 newStr, 4 appends = 5000; 5 6 while (appends--) { 7 strs[strs.length] = str; 8 } 9 10 newStr = strs.join("");以上代码为了让IE7或者更低版本的浏览器性能更优。其他浏览器使用以下代码:1 var str = "I'm a thirty-five character string.", 阅读全文
posted @ 2012-06-25 21:09 小猩猩君 阅读(297) 评论(0) 推荐(0) 编辑
摘要:1 function merge(left, right) { 2 var result = []; 3 4 while (left.length > 0 && right.length > 0) { 5 if (left[0] < right[0]) { 6 result.push(left.shift()); 7 } else { 8 result.push(right.shift()); 9 }10 }11 return result.concat(left... 阅读全文
posted @ 2012-06-24 19:22 小猩猩君 阅读(537) 评论(0) 推荐(0) 编辑
摘要:1 if (value < 6) { 2 if (value < 3) { 3 if (value == 0) { 4 return result0; 5 } else if (value == 1) { 6 return result1; 7 } else { 8 return result2; 9 }10 } else {11 if (value == 3) {12 return result3;1... 阅读全文
posted @ 2012-06-24 17:18 小猩猩君 阅读(327) 评论(0) 推荐(0) 编辑
摘要:1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html;ch 阅读全文
posted @ 2012-06-23 21:17 小猩猩君 阅读(187) 评论(0) 推荐(0) 编辑
摘要:1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html;ch 阅读全文
posted @ 2012-06-23 20:02 小猩猩君 阅读(288) 评论(0) 推荐(0) 编辑
摘要:1 //DOM原生 选择器API,IE8、FF3.5、safari3.1、chrome1、opera10支持 2 var errs = document.querySelectorAll('div.warning,div.notice'); 3 4 //使用getElementsByTagName() 5 var errs = [], 6 divs = document.getElementsByTagName('div'), 7 classname = ''; 8 for (var i = 0, len = divs.length; i < 阅读全文
posted @ 2012-06-23 10:40 小猩猩君 阅读(257) 评论(0) 推荐(0) 编辑
摘要:1 function toArray(coll) { 2 for (var i = 0, a = [], len = coll.length; i < len; i++) { 3 a[i] = coll[i]; 4 } 5 return a; 6 } 7 8 //用法 9 var coll = document.getElementsByTagName('div');10 var arr = toArray(coll); 阅读全文
posted @ 2012-06-22 15:26 小猩猩君 阅读(157) 评论(0) 推荐(0) 编辑
摘要:innerHTML: 1 function tableInnerHTML() { 2 var i, h = ['<table border="1" width="100%">']; 3 h.push('<thead>'); 4 h.push('<tr><th>id<\/th><th>yes?<\/th><th>name<\/th><th>url<\/th><th>actio 阅读全文
posted @ 2012-06-20 15:12 小猩猩君 阅读(1439) 评论(0) 推荐(0) 编辑
摘要:举例:location.href > window.location.href > window.location.href.toString()对象成员嵌套得越深,访问速度就会越慢。 阅读全文
posted @ 2012-06-20 13:23 小猩猩君 阅读(244) 评论(0) 推荐(0) 编辑
摘要:1 function loadScript(url, callback) { 2 var script = document.createElement("script"); 3 script.type = "text/javascript"; 4 5 if (script.readyState) { //IE 6 script.onreadystatechange = function() { 7 if (script.readyState == "loaded" || script.readyState == "comp 阅读全文
posted @ 2012-06-19 16:17 小猩猩君 阅读(189) 评论(0) 推荐(0) 编辑
摘要:1 jQuery.preloadImages = function() {2 for(var i = 0; i < arguments.length; i++) {3 $("<img />").attr('src', arguments[i]);4 }5 };6 //用法7 $.preloadImages('image1.gif', '/path/to/image2.png', 'some/image3.jpg'); 阅读全文
posted @ 2012-06-19 12:50 小猩猩君 阅读(128) 评论(0) 推荐(0) 编辑