0

動的に生成されたリストがあります。これが私の HTML コードです

<ol class="pending">
  <li><a href="#" class="rendered">One</a></li>
  <li><a href="#" class="rendered">Two</a></li>
  <li><a href="#" class="rendered">Three</a></li>
  <li><a href="#" class="rendered">Four</a></li>
  <li><a href="#" class="rendered">Five</a></li>
  <li><a href="#" class="rendered">Six</a></li>
</ol>
<ol class="patched"></ol>

特定のリンクをクリックすると、別のリストに移動するはずです。

/*jslint browser: true*/ /*global  $*/ 
$(document).ready(function(){
    "use strict";
    $('.rendered').on('click', function(){
        $(this).toggleClass("rendered patched");
        //$(this).parent().append($(this).wrap("<li></li>"));
        $(this).appendTo("ol.patched");
    });
});

これまでのところ唯一の問題は、<li> のアンカー値を <li> として新しいリストに追加することです。

私が得続ける結果は

<ol class="pending">
  <li></li>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
</ol> 
<ol class="moved">
  <a href="#" class="dld">One</a>
  <a href="#" class="dld">Two</a>
  <a href="#" class="dld">Three</a>
  <a href="#" class="dld">Four</a>
  <a href="#" class="dld">Five</a>
  <a href="#" class="dld">Six</a>
</ol>

何を誤解しているのかよくわかりませんし.append().appendTo()

4

1 に答える 1

1

アンカーの親を選択して に追加する必要がありますol。JQuery.parent()は要素の親を選択します.

$('.rendered').on('click', function(){
    $(this).toggleClass("rendered patched");
    $(this).parent().appendTo("ol.patched");
});

$('.rendered').on('click', function(){
  $(this).toggleClass("rendered patched");
  $(this).parent().appendTo("ol.patched");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ol class="pending">
  <li><a href="#" class="rendered">One</a></li>
  <li><a href="#" class="rendered">Two</a></li>
  <li><a href="#" class="rendered">Three</a></li>
  <li><a href="#" class="rendered">Four</a></li>
  <li><a href="#" class="rendered">Five</a></li>
  <li><a href="#" class="rendered">Six</a></li>
</ol>
<ol class="patched"></ol>

また、コードを1行で書くこともできます

$('.rendered').on('click', function(){
  $(this).toggleClass("rendered patched").parent().appendTo("ol.patched");
});
于 2016-12-16T05:09:54.077 に答える