Created
November 10, 2013 06:24
-
-
Save cuing/7394567 to your computer and use it in GitHub Desktop.
JavaScript DOM 编程艺术 Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 页面加载完成后调用方法 | |
function addLoadEvent(func) { | |
var oldonload = window.onload; | |
if (typeof oldonload !== 'function') { | |
window.onload = func; | |
} else { | |
window.onload = function() { | |
oldonload(); | |
func(); | |
} | |
} | |
} | |
// 在目标元素后添加新元素 | |
function insertAfter(newElement, targetElement) { | |
var parentNode = targetElement.parentNode; | |
if (parentNode.lastChild == targetElement) { | |
parentNode.appendChild(newElement); | |
} else { | |
parentNode.insertBefore(newElement, targetElement.nextSibling) | |
} | |
} | |
// 获取节点的下一个元素 | |
function getNextElement(node) { | |
if (node.nodeType == 1) { | |
return node; | |
} | |
if (node.nextSibling) { | |
return getNextElement(node.nextSibling); | |
} | |
return null; | |
} | |
// 用DOM给元素添加class属性 | |
function addClass(element, value) { | |
if (!element.className) { | |
element.className = value; | |
} else { | |
var newClassName = element.className; | |
newClassName += ' '; | |
newClassName += value; | |
element.className = newClassName; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment