06 2012 档案
摘要:等项目上线,写点简单的东西练练手 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
阅读全文
摘要:简单的例子: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
阅读全文
摘要: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
阅读全文
摘要: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
阅读全文
摘要: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
阅读全文
摘要: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.",
阅读全文
摘要: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...
阅读全文
摘要: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...
阅读全文
摘要: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
阅读全文
摘要: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
阅读全文
摘要: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 <
阅读全文
摘要: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);
阅读全文
摘要: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
阅读全文
摘要:举例:location.href > window.location.href > window.location.href.toString()对象成员嵌套得越深,访问速度就会越慢。
阅读全文
摘要: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
阅读全文
摘要: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');
阅读全文
摘要:1 (function(){2 console.log(arguments instanceof Array); // false 3 var argsArray = Array.prototype.slice.call(arguments);4 console.log(argsArray instanceof Array); // true 5 }());getElementsByTagName返回的NodeList也可以转化成数组1 Array.prototype.slice.call(nodes)
阅读全文
摘要:原始代码:1 var list = document.getElementById("myList");2 for (var i = 0; i < 10; i++) {3 var item = document.createElement("li");4 list.appendChild(item);5 item.appendChild(document.createTextNode("Item" + i));6 }使用文档碎片(fragment)后的代码1 var list = document.getElementById(
阅读全文
摘要:1 //credit: Speed Up Your Site (New Riders, 2003) 2 var iterations = Math.floor(value.length / 8); 3 var leftover = value.length % 8; 4 var i = 0; 5 6 if (leftover > 0) { 7 do { 8 process(values[i++]); 9 } while (--leftover > 0);10 }11 do {12 process(value[i++]);13 process(...
阅读全文
摘要:原生函数1 function handleKeyPress(event) {2 if (event.KeyCode == 13) {3 var target = EventUtil.getTarget(event);4 var value = 5 * parseInt(target.value);5 if (value > 10) {6 document.getElementById("error-msg").style.display = "block";7 }8 }9 }解耦后的函数 1...
阅读全文
摘要:1 var CookieUtil = { 2 get: function(name) { 3 var cookieName = encodeURIComponent(name) + "=", 4 cookieStart = document.cookie.indexOf(cookieName), 5 cookieValue = null; 6 7 if (cookieStart > -1) { 8 var cookieEnd = document.cookie.indexOf("...
阅读全文
摘要: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
阅读全文
摘要:1 //自定义事件 2 function EventTarget() { 3 this.handlers = {}; 4 } 5 EventTarget.prototype = { 6 constructor: EventTarget, 7 //注册 8 addHandler: function(type, handler) { 9 if (typeof this.handlers[type] == "undefined") {10 this.handlers[type] = [];11 ...
阅读全文
摘要:1 function chunk(array, process, context) { 2 setTimeout(function() { 3 var item = array.shift(); 4 process.call(context, item); 5 6 if (array.length > 0) { 7 setTimeout(arguments.callee, 100); 8 } 9 }, 100);10 }11 12 var data = [12, 123, 1234, 4...
阅读全文
摘要:1 function curry(fn, context) { 2 var args = Array.prototype.slice.call(arguments, 2); 3 return function() { 4 var innerArgs = Array.prototype.slice.call(arguments); 5 var finalArgs = args.concat(innerArgs); 6 return fn.apply(context, finalArgs); 7 }; 8 } 9 funct...
阅读全文
摘要:1 function curry(fn) { 2 var args = Array.prototype.slice.call(arguments, 1); 3 return function() { 4 var innerArgs = Array.prototype.slice.call(arguments); 5 var finalArgs = args.concat(innerArgs); 6 return fn.apply(null, finalArgs); 7 }; 8 } 9 10 //使用方法11 funct...
阅读全文
摘要:1 function bind(fn, context) { 2 return function() { 3 return fn.apply(context, arguments); 4 }; 5 } 6 7 //使用方法 8 var handler = { 9 message: "Event handler",10 handleClick: function() {11 console.log(this.message + ":" + event.type);12 }13 };14 var btn = documen...
阅读全文
摘要:1 function Polygon(sides) { 2 if (this instanceof Polygon) { 3 this.sides = sides; 4 this.getArea = function() { 5 return 0; 6 }; 7 } else { 8 return new Polygon(sides); 9 }10 }11 12 function Rectangle(width, height) {13 Polygon.call(this,...
阅读全文
摘要:1 function Person(name, age, job) { 2 if (this instanceof Person) { 3 this.name = name; 4 this.age = age; 5 this.job = job; 6 } else { 7 return new Person(name, age, job); 8 } 9 }10 11 var person1 = Person("Nicholas", 29, "Software Engineer");12 console.l...
阅读全文
摘要: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
阅读全文
摘要: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
阅读全文
摘要: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
阅读全文
摘要:1 function createXHR(){ 2 if(typeof XMLHttpRequest != "undefined"){ 3 createXHR = function(){ 4 return new XMLHttpRequest(); 5 }; 6 }else if(typeof ActiveXObject != "undefined"){ 7 createXHR = function(){ 8 if(typeof arguments.callee.active...
阅读全文
摘要:1 /* 2 *对表单字段的名称和值进行URL编码,使用和号(&)分割。 3 *不发送禁用的表单字段。 4 *只发送勾选的复选框和单选按钮。 5 *不发送type为"reset"和"button"的按钮。 6 *多选选择框中的每个选中的值单独一个条目。 7 *在单击提交表单的情况下,也会发送提交按钮;否则,不发送提交按钮。也包括type为"image"的<input>元素。 8 *<select>元素的值,就是选中的<option>元素的value特性的值。如果<option>元
阅读全文
摘要: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
阅读全文
摘要: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
阅读全文
摘要: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
阅读全文
摘要: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
阅读全文
摘要:1 //getClipboardText2 EventUtil.addHandler(textbox, "paste", function(event) {3 event = EventUtil.getEvent(event);4 var text = EventUtil.getClipboardText(event);5 6 if (!/^\d*$/.test(text)) {7 EventUtil.preventDefault(event);8 }9 });
阅读全文
摘要:1 var textbox = document.forms[0].elements["textbox1"]; 2 3 //文本框忽略所有输入的非数值 4 EventUtil.addHandler(textbox, "keypress", function(event) { 5 event = EventUtil.getEvent(event); 6 var target = EventUtil.getTarget(event); 7 var charCode = EventUtil.getCharCode(event); 8 9 if (!/\d/.t
阅读全文
摘要: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
阅读全文
摘要: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
阅读全文
摘要: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
阅读全文
摘要: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
阅读全文
摘要:DOM 1 var btn = document.getElementById("myBtn"); 2 3 //创建事件对象 4 var event = document.createEvent("MouseEvents"); 5 6 //初始化事件对象 7 event.initMouseEvent("click", true, true, document.defaultView, 0, 0, 0, 0, 0, false, false, false, false, 0, null); 8 9 //触发事件10 btn.dispat
阅读全文

浙公网安备 33010602011771号