var topClick = function() {
child = this.parentNode.parentNode.parentNode.parentNode;
child.parentNode.removeChild(child);
var theFirstChild = document.querySelector(".m-ctt .slt");
data[child.getAttribute("data-id")].rank = 5;
insertBlogs();
};
如你所见,我的代码有一部分是这样的:
this.parentNode.parentNode.parentNode.parentNode;
是否有另一种优化代码的方法(不使用 jQuery)?
最佳答案
您可以使用非递归辅助函数,例如:
function nthParent(element, n) {
while(n-- && element)
element = element.parentNode;
return element;
}
关于javascript - 如何优化像 "this.parentNode.parentNode.parentNode..."这样的代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35129687/