代码之家  ›  专栏  ›  技术社区  ›  Dominik Svoboda

每个函数仅适用于console.log

  •  0
  • Dominik Svoboda  · 技术社区  · 8 年前

    需要所有人 作为包装文本保存到另一个div。只显示姓氏,但在控制台日志中它正在工作。无法找出问题所在。

    $(".score" ).children('div').each(function(arr) {
      clsas = $(this).attr('class');
      $('.part-list').html(clsas);
      clsas = clsas.replace(/_/gm, " ")
                   .replace(/(\d{1,}) (\d{1,})/gm, "$1, $2")
                   .replace(/stff /gm, "")
                   .replace(/(\B)(\d{1,})/gm, "$1 $2");
      console.log(clsas);
    }).each(function() {
      $('.part-list').html( "<div class='instrument'>" + clsas + "</div>" ); 
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <div class="part-list"></div>
    <div class="score">
    <div class="stff_Flute1_2"></div>
    <div class="stff_Oboe_1_2"></div>
    <div class="stff_Clarinet_1_2"></div>
    </div>

    https://jsfiddle.net/3od8uudf/2/

    2 回复  |  直到 8 年前
        1
  •  1
  •   SDhaliwal    8 年前

    $.html(字符串)设置html的值,因此对于通过它的每个循环,都将html设置为其他内容。

    解决这个问题的一种方法是创建一个新元素并将其附加到所需的位置。

    前任。

    var txt1 = "<div class='instrument'>" + clsas + "</div>" ; // Create element with HTML  
    
    $('.part-list').append(txt1);      // Append the new element
    
        2
  •  0
  •   Pat Mellon    8 年前

    $(".score" ).children('div').each(function( arr ) {
    clsas = $(this).attr('class');
    clsas = clsas.replace(/_/gm, " ").replace(/(\d{1,}) (\d{1,})/gm, "$1, $2").replace(/stff /gm, "").replace(/(\B)(\d{1,})/gm, "$1 $2");
      $('.part-list').append("<div class='instrument'>" + clsas + "</div><br>");
      });
       
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <div class="part-list"></div>
    <div class="score">
    <div class="stff_Flute1_2"></div>
    <div class="stff_Oboe_1_2"></div>
    <div class="stff_Clarinet_1_2"></div>
    </div>