克隆无法正常工作
我正在尝试这段代码,但显然我有一些错误。
问题就在这里,因为没有这部分一切都可以正常工作:
.find('ol:first >li:eq(0)')
.attr('id', 'one' + increment)
.find('ol:first >li:eq(1)')
.attr('id', 'two' + increment)
完整源代码:
<div id="container">
<div id="input0" class="clonedInput">
<br>
<ol id="vall0">
<li id="one0">one</li>
<li id="two0">two</li>
</ol>
<input id="value0" size="20" type="text"/>
</div>
</div>
<script type="text/javascript">
$(document).ready(function() {
var $container = $('#container'),
$clone = $('#input0'),
numClones = 4,
startNumber = 1;
function cloneInput(num, increment, $elem) {
var $newElem = $elem
.clone(true)
.attr('id', 'input' + increment)
.find('ol:first')
.attr('id', 'vall' + increment)
.find('ol:first >li:eq(0)')
.attr('id', 'one' + increment)
.find('ol:first >li:eq(1)')
.attr('id', 'two' + increment)
.end();
$newElem.children(':text')
.prop('id', "value" + increment)
.prop('valor', 'valor')
.val('');
$container.append($newElem);
if (num > 1) {
var next = num - 1;
var incr = increment + 1;
cloneInput(next, incr, $elem);
}
}
cloneInput(numClones, startNumber, $clone);
});
</script>
I am trying this code, but apparently i have some bug.
The problem is here, because without this part all works correctly:
.find('ol:first >li:eq(0)')
.attr('id', 'one' + increment)
.find('ol:first >li:eq(1)')
.attr('id', 'two' + increment)
full source:
<div id="container">
<div id="input0" class="clonedInput">
<br>
<ol id="vall0">
<li id="one0">one</li>
<li id="two0">two</li>
</ol>
<input id="value0" size="20" type="text"/>
</div>
</div>
<script type="text/javascript">
$(document).ready(function() {
var $container = $('#container'),
$clone = $('#input0'),
numClones = 4,
startNumber = 1;
function cloneInput(num, increment, $elem) {
var $newElem = $elem
.clone(true)
.attr('id', 'input' + increment)
.find('ol:first')
.attr('id', 'vall' + increment)
.find('ol:first >li:eq(0)')
.attr('id', 'one' + increment)
.find('ol:first >li:eq(1)')
.attr('id', 'two' + increment)
.end();
$newElem.children(':text')
.prop('id', "value" + increment)
.prop('valor', 'valor')
.val('');
$container.append($newElem);
if (num > 1) {
var next = num - 1;
var incr = increment + 1;
cloneInput(next, incr, $elem);
}
}
cloneInput(numClones, startNumber, $clone);
});
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种可能的解决方案是将您的问题部分更改为:
另请参阅我的 jsfiddle。
One possible solution is to change your problem part to:
Also see my jsfiddle.
.end()
可能没有按照您的想法进行操作。您不需要在像这样的 jQuery 调用链的末尾调用它。
.end()
的作用是,它将有效地将您“弹出”回之前的过滤结果/范围。对.find()
的后续调用将在当前过滤的元素之上执行选择。在进行额外的.find()
调用之前尝试添加.end()
调用:但是,我必须说您没有最有效地使用 jQuery 选择器...
.end()
may not be doing what you think it does.You don't need to call it at the end of a chain of jQuery calls like that.
What
.end()
does is that it will effectively "pop" you back to the previous filter result/scope. Subsequent calls to.find()
will perform the selection on top of the currently filtered elements. Toy around with adding.end()
calls before making additional.find()
calls:However, I must say that you're not making the most efficient use of jQuery selectors...