随笔分类 -  js/jq

摘要: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) 阅读全文
posted @ 2012-06-19 11:10 小猩猩君 阅读(5781) 评论(1) 推荐(0)
摘要:原始代码: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( 阅读全文
posted @ 2012-06-18 16:51 小猩猩君 阅读(311) 评论(0) 推荐(0)
摘要: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(... 阅读全文
posted @ 2012-06-18 16:33 小猩猩君 阅读(355) 评论(0) 推荐(0)
摘要:原生函数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... 阅读全文
posted @ 2012-06-17 22:29 小猩猩君 阅读(239) 评论(0) 推荐(0)
摘要: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("... 阅读全文
posted @ 2012-06-17 19:22 小猩猩君 阅读(185) 评论(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-15 16:10 小猩猩君 阅读(1135) 评论(0) 推荐(0)
摘要: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 ... 阅读全文
posted @ 2012-06-13 15:03 小猩猩君 阅读(177) 评论(0) 推荐(0)
摘要: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... 阅读全文
posted @ 2012-06-12 11:26 小猩猩君 阅读(404) 评论(0) 推荐(0)
摘要: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... 阅读全文
posted @ 2012-06-10 20:07 小猩猩君 阅读(226) 评论(0) 推荐(0)
摘要: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... 阅读全文
posted @ 2012-06-10 18:00 小猩猩君 阅读(316) 评论(0) 推荐(0)
摘要: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... 阅读全文
posted @ 2012-06-10 14:42 小猩猩君 阅读(376) 评论(0) 推荐(0)
摘要: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,... 阅读全文
posted @ 2012-06-08 16:20 小猩猩君 阅读(204) 评论(0) 推荐(0)
摘要: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... 阅读全文
posted @ 2012-06-08 15:59 小猩猩君 阅读(151) 评论(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-08 11:19 小猩猩君 阅读(819) 评论(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-07 14:48 小猩猩君 阅读(422) 评论(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-06 10:05 小猩猩君 阅读(136) 评论(0) 推荐(0)
摘要: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... 阅读全文
posted @ 2012-06-05 20:29 小猩猩君 阅读(508) 评论(0) 推荐(0)
摘要:1 /* 2 *对表单字段的名称和值进行URL编码,使用和号(&)分割。 3 *不发送禁用的表单字段。 4 *只发送勾选的复选框和单选按钮。 5 *不发送type为"reset"和"button"的按钮。 6 *多选选择框中的每个选中的值单独一个条目。 7 *在单击提交表单的情况下,也会发送提交按钮;否则,不发送提交按钮。也包括type为"image"的<input>元素。 8 *<select>元素的值,就是选中的<option>元素的value特性的值。如果<option>元 阅读全文
posted @ 2012-06-04 20:34 小猩猩君 阅读(425) 评论(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-04 20:01 小猩猩君 阅读(204) 评论(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-04 17:21 小猩猩君 阅读(241) 评论(0) 推荐(0)