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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title> Find the parent element </title>
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
</head>
<body>
 <div class="divclass" id="div1" style="width:300px;height:150px;">
  <div  style="width:200px;height:100px; margin-top:10px;margin-left:30px;">
  <p>
     Add a border to the ancestor 
  </p>
  </div>
 </div>
</body>
</html>
 <script>
     $("p").closest("div[id='div1']").css("border", "1px solid blue");
 </script>

Can try to change the closest to parents


Related articles: