Skip to content

Instantly share code, notes, and snippets.

@ethertank
Created February 24, 2012 01:52
Show Gist options
  • Save ethertank/1896639 to your computer and use it in GitHub Desktop.
Save ethertank/1896639 to your computer and use it in GitHub Desktop.
HTMLElement.insertBefore のラッパー関数
挿入先要素の親要素の指定を自動化。
### usage ###
[html]
​<div><div id="target">target</div></div>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
[js]
var doc = document, elm = doc.createElement("div"), targetElm = doc.getElementById("target");
elm.appendChild(doc.createTextNode("insert !"));
​insertBefore(elm, targetElm);​
​function insertBefore(newElm,targetElm) {
targetElm.parentNode.insertBefore(newElm, targetElm);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment