ã°ãã¼ãã«é åã«ä»¥ä¸ã®ä¸è¡ãæ¸ã
<script> var currentScript = (function (e) { if(e.nodeName.toLowerCase() == 'script') return e; return arguments.callee(e.lastChild) })(document); alert(currentScript); </script>
ãããããã¨ã§ãcurrentScript ã¯ãã® script è¦ç´ ãæãã
ãã¡ãã説æ
DOM ã¯æ§ç¯ãããã¨ãã«ãä¸ããé çªã«æ§ç¯ããããããã¦ãscript ã¿ã°ãããã¨ã script è¦ç´ ãæ§ç¯ãããã¨ã«ãã¹ã¯ãªãããå®è¡ããã
ã¤ã¾ããã¹ã¯ãªãããå®è¡ãããã¨ã script è¦ç´ ã¯ä»ã¾ãã«ä½ãããã°ããã§ããããããããå¾ãã®è¦ç´ ãåå¨ããªããããã«ãã® script è¦ç´ ãå«ããã¹ã¦ã®è¦ç´ ã ä»ã¾ãã«æ§ç¯ä¸ã®ç¶æ
㧠document ãã lastChild ããã©ã£ã¦è¡ãã°å¿
ããã©ãçãã®ã§ããã
ãã¨ãã°ã以ä¸ã®ãã㪠HTML ããã£ãã¨ããã¨
<html> <script> // ãã®ã¹ã¯ãªãããå®è¡ãããæç¹ã§ã¯ãã® script è¦ç´ 㯠// document.lastChild.lastChild ã§ããã </script> <head> <script> // ãã®ã¹ã¯ãªãããå®è¡ãããæç¹ã§ã¯ãã® script è¦ç´ 㯠// document.lastChild.lastChild.lastChild ã§ããã </script> </head> <body> <script> // ãã®ã¹ã¯ãªãããå®è¡ãããæç¹ã§ã¯ãã® script è¦ç´ 㯠// document.lastChild.lastChild.lastChild ã§ããã </script> </body> <script> // ãã®ã¹ã¯ãªãããå®è¡ãããæç¹ã§ã¯ãã® script è¦ç´ 㯠// document.lastChild.lastChild ã§ããã </script> </html>
ã¨ãããã¨ã§ããã
ãããå©ç¨ããã¨
script ã¿ã°ãå¼µãã¤ããä½ç½®ã®åããæä½ããã¨ãããã¨ãç°¡åã«åºæ¥ããã§ããã
ä¾ãã°ï¼åã®ã¿ã°ãæ¶ã
// hidden.js var currentScript = (function (e) { if(e.nodeName.toLowerCase() == 'script') return e; return arguments.callee(e.lastChild) })(document); var prevTag = (function(e){ if(e.nodeType == 1) return e; return arguments.callee(e) })(currentScript.previousSibling); prevTag.style.display = 'none';
<div>ç§å¯ã ãã¼ï¼ï¼</div><script src="hidden.js"></script>
ã¨ãã¨ã
追è¨ï¼ prototype.js 1.5 ç³»ãªããã¶ãããããªã(æªç¢ºèª)
Element.recursivelyCollect(document, 'lastChild').last();