社å ã®åå¼·ä¼ã®è³æãããã«å ¬éãã¦ããã¾ãã社å ã®äººã社å¤ã®äººãèªãã§ãã ããã
â»ã¿ã¼ã²ãã㯠JavaScript ã¯æ¸ãããã¨ãªãããªãã¸ã§ã¯ãæåè¨èªããã°ã©ãã
â»ä¿¡å¿µã¯ãæããã®ã§ã¯ãªããå¿
æ»ã«çãã¦ããã¾ããã
ä»é±ã¯ããªããã¼ããã¤ã«ãã§å
¨èº«ã¿ã¤ãã®ãããªå¿ãããªã®ã§ãã¿ããªãæ¥ãåãã¦æ¥½ããã§ããããé¦æ¸¯ã«è¡ã£ã¦æ¥½ããã§ããéã«ããããè³æãä½ã£ã¦ãã¾ãã
ã¯ãã
Section 00 Prototype.js ã®åã« JavaScript 㨠DOM ã¨ã¤ãã³ãã®æ¦è¦
HTML ã¯èªã¿è¾¼ã¾ããå¾ããã¹ã¦ã®æ
å ±ã JavaScript ã®ãªãã¸ã§ã¯ãã«å¤æãããã
ã¤ã¡ã¼ã¸çã«ã¯ãããªæã
<html> <head> <title>ã¿ã¤ãã«</title> </head> <body> ï¼ ï¼ </body> </html>
âââ
var document = { documentElement: { tagName: 'HTML', childNodes: { 0: { tagName: 'HEAD', childNodes: { 0: { tagName: 'TITLE', childNodes: { 0: { nodeValue: 'ã¿ã¤ãã«' } } } } }, 1: { tagName: 'BODY', childNodes:{ ï¼ ï¼ } } } } };
ãã®ä»çµã¿ã DOM ã¨è¨ããW3C ã¨ããæ¨æºåå£ä½ã«ãã£ã¦æ¨æºåããã¦ããã
ãã¨ãã° html è¦ç´ ã«å¯¾å¿ãããªãã¸ã§ã¯ã㯠document.documentElement ã§åå¾ã§ããã
以ä¸ã®ããã«ãã㨠html è¦ç´ ã®ä¸ã«å«ã¾ããã½ã¼ã¹ãæååã¨ãã¦è¦ããã¨ãåºæ¥ãã
<html><head><script> alert(document.documentElement.innerHTML); </script><body>Sample Sample Sample</body></html>
ããããä¸ã® JavaScript ãå®è¡ããã¨ãä¸å®å
¨ãª( body ããªã) HTML ã表示ãããã
ããã¯ãscript è¦ç´ ãèªã¿è¾¼ã¾ããæç¹ã§ã¹ã¯ãªãããå®è¡ããã以å¾ã®ã½ã¼ã¹ããªãã¸ã§ã¯ãåããã¦ããªãããã§ããã
DOM ã®ç¶æ ãä¸å®å ¨ã§ã¯ã³ã³ãã³ããèªç±ã«æããã¨ãåºæ¥ãªãããã DOM ãæ§ç¯ãããã¿ã¤ãã³ã°ã§å¦çãéå§ããªããã°ãªããªãã
// ä¸çªå¤ãæ¹æ³ï¼ã¤ãã³ããªã¹ãã¯ã²ã¨ã¤ããç»é²ã§ããªãï¼ window.onload = function() { alert('ãã¼ãããã'); };
// W3C ã«ãã£ã¦æ¨æºåãããæ¹æ³ã IE ã§ã¯æªå¯¾å¿ window.addEventListener('load', function() { alert('ãã¼ãããã'); }, false);
// IE ã®ã¿ window.attachEvent('onload', function() { alert('ãã¼ãããã'); });
ä¸ã®ã½ã¼ã¹ã§ãã£ã¦ããã¨ãã window ãªãã¸ã§ã¯ãã§çºçãã 'load' ã¤ãã³ããã¤ãã³ããªã¹ã(å®ã¯ãã ã®é¢æ°ãªãã¸ã§ã¯ã)ã«ãã£ã¦ç£è¦ãããã¨è¡¨ç¾ããã
ã¤ãã³ã㯠DOM ã®å
¨ã¦ã®ãªãã¸ã§ã¯ãã«çºçãããã®ã§ 'load' 以å¤ã«ãããããããã
Section 01 Event.observe
Source 01 é常ã®ã¤ãã³ããªã¹ãç»é²
var ColorfulBlock = Class.create(); ColorfulBlock.prototype = { element: null, r: 0, g: 0, b: 0, initialize: function(id) { this.element = document.getElementById(id); if(this.element.addEventListener) { this.element.addEventListener('mouseover', this.onMouseOver.bind(this), false); this.element.addEventListener('mouseout', this.onMouseOut.bind(this), false); this.element.addEventListener('mousemove', this.onMouseMove.bind(this), false); } else if(this.element.attachEvent) { this.element.attachEvent('onmouseover', this.onMouseOver.bind(this)); this.element.attachEvent('onmouseout', this.onMouseOut.bind(this)); this.element.attachEvent('onmousemove', this.onMouseMove.bind(this)); } this.setColor({r: 0.5, g: 0.5, b: 0.5}); }, setColor: function(color) { Object.extend(this, color); var array = ['rgb(', 100 - this.r * 20, '%,', 100 - this.g * 20, '%,', 100 - this.b * 20, '%)']; this.element.style.backgroundColor = array.join(''); }, onMouseMove: function(event) { event = event || window.event; this.setColor({r: (event.offsetX || event.layerX || 0) / 400, g: (event.offsetY || event.layerY || 0) / 400}); }, onMouseOver: function(event) { event = event || window.event; this.setColor({r: (event.offsetX || event.layerX || 0) / 400, g: (event.offsetY || event.layerY || 0) / 400, b: 0}); }, onMouseOut: function(event) { event = event || window.event; this.setColor({r: 0.5, g: 0.5, b: 0.5}); } }; var main = function() { new ColorfulBlock('element'); }; if(window.addEventListener) { window.addEventListener('load', main, false); } else if(window.attachEvent) { window.attachEvent('onload', main); }
Source 02 Event.observe ã使ã£ã¦ã¤ãã³ããªã¹ããç»é²ããã
var ColorfulBlock = Class.create(); ColorfulBlock.prototype = { element: null, r: 0, g: 0, b: 0, initialize: function(id) { this.element = document.getElementById(id); Event.observe(this.element, 'mouseover', this.onMouseOver.bind(this)); Event.observe(this.element, 'mouseout', this.onMouseOut.bind(this)); Event.observe(this.element, 'mousemove', this.onMouseMove.bind(this)); this.setColor({r: 0.5, g: 0.5, b: 0.5}); }, setColor: function(color) { Object.extend(this, color); var array = ['rgb(', 100 - this.r * 20, '%,', 100 - this.g * 20, '%,', 100 - this.b * 20, '%)']; this.element.style.backgroundColor = array.join(''); }, onMouseMove: function(event) { event = event || window.event; this.setColor({r: (event.offsetX || event.layerX || 0) / 400, g: (event.offsetY || event.layerY || 0) / 400}); }, onMouseOver: function(event) { event = event || window.event; this.setColor({r: (event.offsetX || event.layerX || 0) / 400, g: (event.offsetY || event.layerY || 0) / 400, b: 0}); }, onMouseOut: function(event) { event = event || window.event; this.setColor({r: 0.5, g: 0.5, b: 0.5}); } }; Event.observe(window, 'load', function() { new ColorfulBlock('element'); });
Source 03 ãã¾ãã $H ã使ã£ã¦ãªãã¡ã¯ã¿ãªã³ã°( Event.observe ã¨é¢ä¿ãªããã©)
var ColorfulBlock = Class.create(); ColorfulBlock.prototype = { element: null, r: 0, g: 0, b: 0, eventLinteners: { mouseover: 'onMouseOver', mouseout: 'onMouseOut', mousemove: 'onMouseMove' }, initialize: function(id) { this.element = document.getElementById(id); $H(this.eventLinteners).each(function(set) { Event.observe(this.element, set.key, this[set.value].bind(this)); }.bind(this)); this.setColor({r: 0.5, g: 0.5, b: 0.5}); }, setColor: function(color) { Object.extend(this, color); var array = ['rgb(', 100 - this.r * 20, '%,', 100 - this.g * 20, '%,', 100 - this.b * 20, '%)']; this.element.style.backgroundColor = array.join(''); }, onMouseMove: function(event) { event = event || window.event; this.setColor({r: (event.offsetX || event.layerX || 0) / 400, g: (event.offsetY || event.layerY || 0) / 400}); }, onMouseOver: function(event) { event = event || window.event; this.setColor({r: (event.offsetX || event.layerX || 0) / 400, g: (event.offsetY || event.layerY || 0) / 400, b: 0}); }, onMouseOut: function(event) { event = event || window.event; this.setColor({r: 0.5, g: 0.5, b: 0.5}); } }; Event.observe(window, 'load', function() { new ColorfulBlock('element'); });
Section 02 $ é¢æ°
Source 01 é常ã®è¦ç´ åå¾( Section 01 ã® Source 03 ã¨åã)
var ColorfulBlock = Class.create(); ColorfulBlock.prototype = { element: null, r: 0, g: 0, b: 0, eventLinteners: { mouseover: 'onMouseOver', mouseout: 'onMouseOut', mousemove: 'onMouseMove' }, initialize: function(id) { this.element = document.getElementById(id); $H(this.eventLinteners).each(function(set) { Event.observe(this.element, set.key, this[set.value].bind(this)); }.bind(this)); this.setColor({r: 0.5, g: 0.5, b: 0.5}); }, setColor: function(color) { Object.extend(this, color); var array = ['rgb(', 100 - this.r * 20, '%,', 100 - this.g * 20, '%,', 100 - this.b * 20, '%)']; this.element.style.backgroundColor = array.join(''); }, onMouseMove: function(event) { event = event || window.event; this.setColor({r: (event.offsetX || event.layerX || 0) / 400, g: (event.offsetY || event.layerY || 0) / 400}); }, onMouseOver: function(event) { event = event || window.event; this.setColor({r: (event.offsetX || event.layerX || 0) / 400, g: (event.offsetY || event.layerY || 0) / 400, b: 0}); }, onMouseOut: function(event) { event = event || window.event; this.setColor({r: 0.5, g: 0.5, b: 0.5}); } }; Event.observe(window, 'load', function() { new ColorfulBlock('element'); });
Source 02 $ é¢æ°ã使ã£ãè¦ç´ åå¾ãdocument.getElementById ããç°¡åã
var ColorfulBlock = Class.create(); ColorfulBlock.prototype = { element: null, r: 0, g: 0, b: 0, eventLinteners: { mouseover: 'onMouseOver', mouseout: 'onMouseOut', mousemove: 'onMouseMove' }, initialize: function(element) { this.element = $(element); $H(this.eventLinteners).each(function(set) { Event.observe(this.element, set.key, this[set.value].bind(this)); }.bind(this)); this.setColor({r: 0.5, g: 0.5, b: 0.5}); }, setColor: function(color) { Object.extend(this, color); var array = ['rgb(', 100 - this.r * 20, '%,', 100 - this.g * 20, '%,', 100 - this.b * 20, '%)']; this.element.style.backgroundColor = array.join(''); }, onMouseMove: function(event) { event = event || window.event; this.setColor({r: (event.offsetX || event.layerX || 0) / 400, g: (event.offsetY || event.layerY || 0) / 400}); }, onMouseOver: function(event) { event = event || window.event; this.setColor({r: (event.offsetX || event.layerX || 0) / 400, g: (event.offsetY || event.layerY || 0) / 400, b: 0}); }, onMouseOut: function(event) { event = event || window.event; this.setColor({r: 0.5, g: 0.5, b: 0.5}); } }; Event.observe(window, 'load', function() { new ColorfulBlock('element'); });
上の例を実行
ããã«ã $ é¢æ°ã使ããã¨ã«ãã£ã¦ãã³ã³ã¹ãã©ã¯ã¿ã«ä¸ããããå¼æ°ãæååã§ãè¦ç´ ãªãã¸ã§ã¯ãã§ãåä½ããããã«ãªãã
Section 03 Function.prototype.bindAsEventListener
Source 01 é常ã®ã¤ã³ã¹ã¿ã³ã¹ã¡ã½ããã«ããã¤ãã³ããªã¹ã( Section 02 ã® Source 02 ã¨åã)
var ColorfulBlock = Class.create(); ColorfulBlock.prototype = { element: null, r: 0, g: 0, b: 0, eventLinteners: { mouseover: 'onMouseOver', mouseout: 'onMouseOut', mousemove: 'onMouseMove' }, initialize: function(element) { this.element = $(element); $H(this.eventLinteners).each(function(set) { Event.observe(this.element, set.key, this[set.value].bind(this)); }.bind(this)); this.setColor({r: 0.5, g: 0.5, b: 0.5}); }, setColor: function(color) { Object.extend(this, color); var array = ['rgb(', 100 - this.r * 20, '%,', 100 - this.g * 20, '%,', 100 - this.b * 20, '%)']; this.element.style.backgroundColor = array.join(''); }, onMouseMove: function(event) { event = event || window.event; this.setColor({r: (event.offsetX || event.layerX || 0) / 400, g: (event.offsetY || event.layerY || 0) / 400}); }, onMouseOver: function(event) { event = event || window.event; this.setColor({r: (event.offsetX || event.layerX || 0) / 400, g: (event.offsetY || event.layerY || 0) / 400, b: 0}); }, onMouseOut: function(event) { event = event || window.event; this.setColor({r: 0.5, g: 0.5, b: 0.5}); } }; Event.observe(window, 'load', function() { new ColorfulBlock('element'); });
Source 02 Function.prototype.bindAsEventListener ã使ã£ãã¤ãã³ããªã¹ã
var ColorfulBlock = Class.create(); ColorfulBlock.prototype = { element: null, r: 0, g: 0, b: 0, eventLinteners: { mouseover: 'onMouseOver', mouseout: 'onMouseOut', mousemove: 'onMouseMove' }, initialize: function(element) { this.element = $(element); $H(this.eventLinteners).each(function(set) { Event.observe(this.element, set.key, this[set.value].bindAsEventListener(this)); }.bind(this)); this.setColor({r: 0.5, g: 0.5, b: 0.5}); }, setColor: function(color) { Object.extend(this, color); var array = ['rgb(', 100 - this.r * 20, '%,', 100 - this.g * 20, '%,', 100 - this.b * 20, '%)']; this.element.style.backgroundColor = array.join(''); }, onMouseMove: function(event) { this.setColor({r: (event.offsetX || event.layerX || 0) / 400, g: (event.offsetY || event.layerY || 0) / 400}); }, onMouseOver: function(event) { this.setColor({r: (event.offsetX || event.layerX || 0) / 400, g: (event.offsetY || event.layerY || 0) / 400, b: 0}); }, onMouseOut: function(event) { this.setColor({r: 0.5, g: 0.5, b: 0.5}); } }; Event.observe(window, 'load', function() { new ColorfulBlock('element'); });