代码示例:(标识:jsck_element_closest_1)
<!DOCTYPE html>
<html>
<style>
.container, .demo {
  background-color: tomato;
  color: white;
  padding: 20px;
  margin: 10px;
}
</style>

<body>
<h1>Element 对象</h1>

<h2>The closest() Method</h2>

<div class="demo">祖父
  <div class="demo container">父
    <div id="myElement" class="demo">将选择此 div 元素的父元素。</div>
  </div>
</div>

<script>
const element = document.getElementById("myElement");
const closest = element.closest(".container");
if (closest) {
  closest.style.border = "4px solid black";
}
</script>

</body>
</html>
运行结果: