循环函数 class 名称 jquery

Loop function class name with jquery

我必须遵循以下代码:

$( ".content.one" ).clone().appendTo( ".cat.one .details" );
$( ".content.two" ).clone().appendTo( ".cat.two .details" );
$( ".content.three" ).clone().appendTo( ".cat.three .details" );

我想这样循环

var obj = {
one: 'one',
two: 'two',
three: 'three'
};
$.each(obj, function (index, value) {
  $( ".content.(value)" ).clone().appendTo( ".cat.(value) .details" );
});

但是我找不到如何使用 类

中的 "value"

使用Template literals

var obj = {one: 'one',two: 'two',three: 'three'};

$.each( obj, function( key, value ) {
  $(`.content.${value}`).clone().appendTo(`.cat.${value} .details`);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
<div class="content one">One</div>
<div class="content two">Two</div>
<div class="content three">Three</div>

<br/>
<div class="cat one"><div class="details"></div></div>
<div class="cat two"><div class="details"></div></div>
<div class="cat three"><div class="details"></div></div>

注意obj是一个对象并且根据jQuery.each,回调将被传递keyvalue 作为参数而不是 indexvalue.

您可以使用下面给出的代码。

$(".content ." + value).clone().appendTo(".cat ." + value + " .details");

如果请求共享整个代码没有帮助。