Vanilla JavaScript .closest without jQuery

前端 未结 3 794
庸人自扰
庸人自扰 2020-12-07 00:34

I\'m working on an application that only has jQuery 1.1 installed which dosn\'t support the .closest method.

My script currently looks like this:

$(\         


        
3条回答
  •  无人及你
    2020-12-07 01:05

    myImage.parentNode;
    

    Is as vanilla as it gets. Whack that in a loop until you hit the required tag type:

    while(thisTag.parentNode.tagName !== 'TD') // uppercase in HTML, lower in XML
        {
         thisTag=thisTag.parentNode;
        }
    

    That should do the trick in plain old js.

    Danny

提交回复
热议问题