Check if class exists somewhere in parent - vanilla JS

前端 未结 10 837
时光说笑
时光说笑 2020-12-09 08:42

I\'m really struggling to see how to do this. I want to check if a class exsits somewhere in one of the parent elements of an element.

I don\'t want to use any libra

10条回答
  •  青春惊慌失措
    2020-12-09 08:44

    My example for Vanilla JS, it's use a vanilla equivalent of parents() from jQuery

    var htmlElement = ,
        parents = [],
        classExist;
    while (htmlElement = htmlElement.parentNode.closest()) {
        parents.push(htmlElement);
    }
    classExist = (parents > 0);
    

    So your selector just to be a .className

    And just check if parent is > 0

提交回复
热议问题