Created
December 29, 2010 20:06
-
-
Save think49/758999 to your computer and use it in GitHub Desktop.
getAncestorElementByTagName.js : 対象の tagName を持つ祖先要素ノードを返す。存在しなければ null を返す。
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
/** | |
* getAncestorElementByTagName.js | |
* | |
* @version 1.0 | |
* @author think49 | |
*/ | |
/** | |
* get Ancestor Element by tagName. | |
* @function | |
* @param {Object} node DOM Node. | |
* @param {String} tagName Tag Name. | |
* @returns Ancestor element node. If a target node is not found, return null. | |
* @type {Object}, {Boolean} | |
*/ | |
function getAncestorElementByTagName (node, tagName) { | |
if (typeof node !== 'object' || !node) { | |
return null; | |
} | |
tagName = (tagName + '').toLowerCase(); | |
while (node && node.parentNode) { | |
node = node.parentNode; | |
if (node.tagName && node.tagName.toLowerCase() === tagName) { | |
break; | |
} | |
} | |
return node; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment