如何使用 jQuery 克隆、修改和添加元素?
我想克隆并添加一个 div,但是我想更改添加的 div 名称 每次迭代时从 preConfi 到 preConfiXX(例如 preConfiA1、preConfiB1)。我可以为此使用什么语法?
/* on initial load, move default template into each GroupX location */
var groups = ['A','B','C','D','E','F','G']
for (var groupLetter in groups){
$('#template').clone().prependTo('#placeholder' + groups[groupLetter]);
}
<!-- ************************** -->
<!-- ******* GROUPS ******** -->
<!-- ************************** -->
<div id='groupA' class='preGroups'>
GroupA
<div id="placeholderA"></div>
</div>
<div id='groupB' class='preGroups'>
GroupB
<div id="placeholderB"></div>
</div>
....
<div id='groupF' class='preGroups'>
<div id="placeholderF"></div>
GroupF
</div>
<div id='template'>
<input type="radio" data-theme="a" name="preConfi" id="radio-choice-1" value="C" />
<input type="radio" data-theme="a" name="preConfi" id="radio-choice-2" value="T" />
<input type="radio" data-theme="a" name="preConfi" id="radio-choice-3" value="P" />
</div>
I want to clone and prepend a div, however I want to change the prepended div name
from preConfi to preConfiXX (eg preConfiA1, preConfiB1) on each iteration. What syntax can I use for this?
/* on initial load, move default template into each GroupX location */
var groups = ['A','B','C','D','E','F','G']
for (var groupLetter in groups){
$('#template').clone().prependTo('#placeholder' + groups[groupLetter]);
}
<!-- ************************** -->
<!-- ******* GROUPS ******** -->
<!-- ************************** -->
<div id='groupA' class='preGroups'>
GroupA
<div id="placeholderA"></div>
</div>
<div id='groupB' class='preGroups'>
GroupB
<div id="placeholderB"></div>
</div>
....
<div id='groupF' class='preGroups'>
<div id="placeholderF"></div>
GroupF
</div>
<div id='template'>
<input type="radio" data-theme="a" name="preConfi" id="radio-choice-1" value="C" />
<input type="radio" data-theme="a" name="preConfi" id="radio-choice-2" value="T" />
<input type="radio" data-theme="a" name="preConfi" id="radio-choice-3" value="P" />
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会尝试一下我认为你想要的:
http://jsfiddle.net/a5HB7/3/< /a>
I'll take a shot at what I think you want:
http://jsfiddle.net/a5HB7/3/