📜  javascript 节点有父类 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:03:23.578000             🧑  作者: Mango

代码示例1
/**
     * If the element/node ('child') has the class 'classname' => return true
     * Else: call the function again with parent until parent with class is found or no parent is left
*/
function hasParentClass(child, classname){
  if(child.className.split(' ').indexOf(classname) >= 0) return true;
  try{
    //Throws TypeError if child doesn't have parent any more
    return child.parentNode && hasParentClass(child.parentNode, classname);
  }catch(TypeError){
    return false;
  }
}