最终结果:

jquery 获取所有父元素

 

代码:

 

<!DOCTYPE html>
<html>
<head>
  <style>
  b, span, p, html body {
    padding: .5em;
    border: 1px solid;
  }
  b { color:blue; }
  strong { color:red; }
  </style>
  <script type="text/javascript" src="/jquery/jquery.js"></script>
</head>

<body>
  <div>
    <p>
      <span>
        <b>我的父元素是:</b>
      </span>
      <i>4</i>
    </p>
  </div>

<script>
var parentEls = $("b").parents()
            .map(function () { 
                  return this.tagName; 
                })
            .get().join(", ");
$("b").append("<strong>" + parentEls + "</strong>");
</script>

</body>
</html>

 

相关文章:

  • 2022-01-29
  • 2022-12-23
  • 2021-04-30
  • 2021-12-26
  • 2022-12-23
  • 2021-12-26
  • 2022-12-23
猜你喜欢
  • 2021-08-30
  • 2021-09-02
  • 2022-12-23
  • 2021-12-26
  • 2021-12-22
  • 2021-12-26
  • 2021-12-19
相关资源
相似解决方案