简体   繁体   English

每个功能仅对console.log有效

[英]Each function is working only for console.log

Need to all classnames be saved as wrapped text to another div. 需要将所有类名另存为包装文本到另一个div。 Only last classname showing, but in console log it is working. 仅显示最后一个类名,但在控制台日志中它正在工作。 Cannot figured out what is wrong. 无法找出问题所在。

 $(".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/ https://jsfiddle.net/3od8uudf/2/

$.html( string ) sets the value of the html, so for each loop through it is setting the html to something else. $ .html(string)设置html的值,因此对于每次循环,都将html设置为其他值。

In your example it would be set to flute, then to oboe and then to clarinet. 在您的示例中,它将设置为长笛,然后是双簧管,然后是单簧管。

A way around this would be to create a new element and append it to where you want. 一种解决方法是创建一个新元素并将其附加到所需的位置。

Ex. 例如

var txt1 = "<div class='instrument'>" + clsas + "</div>" ; // Create element with HTML  

$('.part-list').append(txt1);      // Append the new element

 $(".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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM