3 回答

TA貢獻1878條經驗 獲得超4個贊
您正在尋找parentNode
,其Element
繼承自Node
:
parentDiv = pDoc.parentNode;
方便的參考:
DOM2 Core規范-得到所有主流瀏覽器的良好支持
DOM2 HTML規范-DOM和HTML之間的綁定
DOM3 Core規范-一些更新,并非所有主流瀏覽器都支持所有更新
HTML5規范 -現在包含DOM / HTML綁定

TA貢獻1796條經驗 獲得超7個贊
如果您正在尋找一種比直接父元素更遠的元素,可以使用一個在DOM上查找直到找到一個或沒有找到的函數:
// Find first ancestor of el with tagName
// or undefined if not found
function upTo(el, tagName) {
tagName = tagName.toLowerCase();
while (el && el.parentNode) {
el = el.parentNode;
if (el.tagName && el.tagName.toLowerCase() == tagName) {
return el;
}
}
// Many DOM methods return null if they don't
// find the element they are searching for
// It would be OK to omit the following and just
// return undefined
return null;
}
添加回答
舉報