jquery - 带有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" );
});
但我不知道如何在类中使用“值”
解决方案
使用模板文字
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,回调将作为参数传递key
,value
而不是index
and value
。
推荐阅读
- javascript - 看不见的recaptcha是可见的
- r - 发散条形图 - 添加文本标签,根据它是负数还是正数进行调整
- ios - 如何从实时数据库 Firebase 观察更多级别?
- python-3.x - 我想从列表中选择我的 id 符合条件的表
- javascript - 如何在何时获取值是空的
- web-applications - 调试运行 Tizen Web 应用程序...?
- swift - 在特定时间重复本地通知,并在同一时间间隔后重复
- reactjs - 如何模拟 i18n.addResourceBundle?
- scala - 通过时间戳scala更新数据帧值
- php - 如何在 PHP 中使用多个搜索条件查找相关库存?