Find the difference between the parents and the closest approach of jquery

  • 2020-03-30 00:38:51
  • OfStack

The former can return multiple elements or only 0 or 1

The former is all the superior elements that go all the way to the root which is generally the body  The latter is until the discovery, the discovery of a stop

The former begins with the parent element   The latter starts with itself

The code is as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
    <title> Find the parent element </title>
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
 <div class="divclass" id="div1" style="width:300px;height:150px;">
  <div  style="width:200px;height:100px; margin-top:10px;margin-left:30px;">
     Add a border to the ancestor 
     $("p").closest("div[id='div1']").css("border", "1px solid blue");

Can try to change the closest to parents

Related articles: