jQueryããå¦ãã åå空éãç¯ç´ããæ¹æ³
å®è£ å´ã§ã¯é常ã«è¦å´ãã¾ããã使ãåæã¯æ群ãããããnewæ¼ç®åã使ã£ã¦ã¤ã³ã¹ã¿ã³ã¹çæãããªãã¦ãé¢æ°ã®æ»ãå¤ã¨ãã¦ãªãã¸ã§ã¯ãã使ããã¯ããã¯ã§ãã
ä¾ã使ã£ã¦èª¬æãã¾ãã以ä¸ã¯DOMã®æä½ã§è¦ç´ ãåé¤ããæ¹æ³ã§ãã
æ®éã®æ¹æ³
var elem = document.getElementById('navi'); elem.parentNode.removeChild(elem);
DOMã®APIã§ã¯ç´æ¥è¦ç´ ãåé¤ã§ãã¾ããããã£ããparentNodeããã©ããremoveChildã§DOMããªã¼ããåé¤ãã¾ãã
æ®éã®ã¯ã©ã¹å
var Elem = function(id){ this.elem=document.getElementById(id); }; Elem.prototype.remove = function(){ this.elem.parentNode.removeChild(this.elem); }; var elem = new Elem('navi'); elem.remove();
æ®éã®OOPçã«ã¯ä½ã®åé¡ãç¡ãåããããããªãã¾ããï¼ã§ããè¦ç´ ãåé¤ããã ãã§å¤æ°ãå®ç¾©ãããã¨ã«ï¼ãããï¼è¡ï¼ã
jQueryã§ãã£ã¦ãæ¹æ³
var Elem = function(id){ if ( window == this || !this.init ) return new Elem(id); return this.init(id); }; Elem.prototype.init = function(id){ this.elem=document.getElementById(id); return this; }; Elem.prototype.remove = function(){ this.elem.parentNode.removeChild(this.elem); }; Elem('navi').remove();
ãªãã¨ã¯ã³ã©ã¤ãã§ãããã³ã¬ã ãç°¡æ½ã«ãããã°ã³ã¼ããªã¼ãã£ã³ã°ãã©ã¯ã©ã¯ã
æ ¸å¿é¨ã¯ä¸è¨ã®ã³ã¼ãã§ä»¥ä¸ã®ããããã®æ¡ä»¶æã«ã¤ã³ã¹ã¿ã³ã¹åããããªãã¸ã§ã¯ããè¿ãããããã«ãªãã¾ãã
- thisãwindowã®ã¨ã(é¢æ°ã¨ãã¦ã³ã¼ã«ãããå ´å㯠thisãwindowã«ãªãã¾ãã)
- this.initãæªå®ç¾©ã®ã¨ã(é¢æ°ã¨ãã¦ã³ã¼ã«ãããå ´åãinitãªãã¦ããããã£ã¯å®ç¾©ãã¦ãªãéãç¡ã)
if ( window == this || !this.init ) return new Elem(id);
ãã£ã±ãjQueryæé«ã
ãã®ä»
jQueryã®æåã®æ¹ã«ããä¸è¨ã®ã³ã¼ããæå³ä¸æãã ããæãã¦ã¼
id:asiamothããã®ãããã§ç解ãã¾ãããåæã®JavaScriptã«ã¯window.undefinedããªãã£ããããããã®åæåã§ãã
// Global undefined variable window.undefined = window.undefined;