如何在jQuery中将创建的var元素相互叠加?

时间:2013-06-02 07:24:31

标签: javascript jquery append

所以我有一些vars创建元素:

var dest = $("<div/>", {
  className: "something"});
var desul = $("<ul/>", {
  className: "list"});
var desLi = $("<li/>", {
  className: "list-item"}); 
var desa = $("<a/>", {
  href: "",
  className: "some-link",
  content: "Some Link"});

现在我做一个追加:

$("#target").append(???);

我最终想要的是:

<div id="target">
  <div class="something">
    <ul class="list">
      <li class="list-item">
        <a href="" class="some-link">Some Link</a>
      </li>
    </ul>
  </div>
</div>

那么如何为#target.append编写jQuery?

2 个答案:

答案 0 :(得分:2)

$("#target").append(dest.append(desul.append(desLi.append(desa))));

答案 1 :(得分:1)

你可以像下面这样做,不要在变量中分开(除非你需要):

$("#target").append(
    $("<div/>", {className: "something"}).append(
    $("<ul/>", {className: "list"}).append(
    $("<li/>", {className: "list-item"}).append(
    $("<a/>", {href: "", className: "some-link",content: "Some Link"})))));