代码之家  ›  专栏  ›  技术社区  ›  Aaron Yodaiken

如何使用jquery为元素创建新的父类?

  •  1
  • Aaron Yodaiken  · 技术社区  · 15 年前

    所以我有点像

    <div>
      <!-- other stuff then-->
      <button>Hey</button>
    </div>
    

    我想这样做(使用Javascript/JQuery)

    <div>
      <!-- other stuff then-->
      <div class="bw">
        <button>Hey</button>
      </div>
    </div>
    

    有可能吗?我在网上找不到任何关于它的资料,但是,我是一个完全的jsnoob,所以我可能没有使用正确的术语。谢谢!

    1 回复  |  直到 15 年前
        1
  •  4
  •   Nick Craver    15 年前

    你可以用 .wrap()

    $("button").wrap("<div class='bw'></div>");
    

    You can test it here ,对于包装多个元素,请使用 .wrapAll() ,这是 a full list of the wrapping functions