0

<li>内部に JQuery コードを含むアイテムがあります。これを複製して、 cloned 内<li>で変更したい。出来ますか?$.getJSON url<li>

ここにコード:

<ul class="exercise2">2:2 Exercise - 1

            <li class="exset327" id="exset327"><var class="exset">327</var>:Сет - <var class="setnum">1</var>
              <a class="DelSet" href="#">[-]</a>
              <script>
                $(".exset327 a").click(function () {
                  $.get("/workouts/del_set/327/"); // Here I want to change '327' in cloned item to some variable then clone
                  $(this).parent().remove();
                  $(".exercise2 li").each(function(){
                    $(".setnum",this).html($(this).index() + 1);
                  });
                  return false;
                });
              </script>
            </li>

        <a class="AddSet" href="#">Add set</a>   // link which make a clone of above <li>
        <script>
          $(".exercise2 a.AddSet").click(function () {
            $.getJSON("/workouts/add_set/2/", function (json) {
              //alert(json.newsetnum);
              var newsetid = json.newsetid;         // returnet from server
              var newsetnum = json.newsetnum;       // returned from server
            // clone
            var clonedLi = $(".exercise2 li:last").clone(true); // 'true' to clone with event handler
            // change attributes
            $(clonedLi).attr("id", "exset" + newsetid);
            $(clonedLi).attr("class", "exset" + newsetid);
            // change content
            $(clonedLi).find(".exset").html(newsetid);
            $(clonedLi).find(".setnum").html(newsetnum);
            // add
            $(clonedLi).insertAfter(".exercise2 li:last");
            });
          });
        </script>
</ul>

複製されたリンクをクリックURLして(削除)変更しない場合は、以前の削除です。[-]<li>

4

1 に答える 1

0

あなたのやり方は最適とは思えないので、代替ルートでお答えします。DOM 要素の複製は問題ありませんが、タグの複製はあまりエレガントではありません。

同じアクションを実行しているように見えますが、要素または変数が異なります。これらの種類の作業をジェネリック クラスで行うための優れた方法があります。

HTML

<li class="exset327" id="exset327">
  <var class="exset">327</var>:Сет - <var class="setnum">1</var>
  <a class="DelSet" href="#" data-exset="327" >[-]</a>
</li>

そしてJS:

$(document).on("click", ".DelSet", function() {
    var exset = $(this).data("exset");

    $.get("/workouts/del_set/"+exset+"/"); 
    $(this).parent().remove();
    $(".exercise2 li").each(function(){
      $(".setnum",this).html($(this).index() + 1);
    });
    return false;
 });

JavaScript のこの部分は、すべての新しい ".DelSet" にクリック リスナーがあることを確認し、以下のコードを実行します。

したがって、あなたがしなければならないことは、< li > のクローンを作成し、data-exset を変更することだけです。

$(clonedLi).find(".DelSet").data("exset", netsetid);
于 2012-10-24T14:37:14.403 に答える