0

所以我有一些创建元素的变量:

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 呢?

4

2 回答 2

2
$("#target").append(dest.append(desul.append(desLi.append(desa))));
于 2013-06-02T07:27:18.460 回答
1

您可以像下面那样进行操作,而无需将其分隔为变量(除非您需要):

$("#target").append(
    $("<div/>", {className: "something"}).append(
    $("<ul/>", {className: "list"}).append(
    $("<li/>", {className: "list-item"}).append(
    $("<a/>", {href: "", className: "some-link",content: "Some Link"})))));
于 2013-06-02T07:28:02.143 回答