克隆元素并将其添加到另一个元素中
考虑以下 HTML 标记:
<form action="">
<div class="row subtitle">
<span>Some stuff.</span>
</div>
<div class="more_subtitles"></div>
<a href="#" class="white add_more"><span>Add more</span></a>
</form>
和 jQuery:
$('.add_more').live('click', function() {
var el_of_form, subtitle, more_subtitles;
el_of_form = $(this).closest('form');
subtitle = $(el_of_form).find('.subtitle');
more_subtitles = $(el_of_form).find('.more_subtitles');
console.log(subtitle);
console.log(more_subtitles);
$(more_subtitles).add(subtitle);
return false;
});
为什么它不起作用? console.log()
找到这些元素...
我想要完成的是,当按下按钮时,它克隆 subtitle
元素并将其添加到 more_subtitles< /代码> 元素。用户可以一次又一次地重复它。
Consider following HTML markup:
<form action="">
<div class="row subtitle">
<span>Some stuff.</span>
</div>
<div class="more_subtitles"></div>
<a href="#" class="white add_more"><span>Add more</span></a>
</form>
And jQuery:
$('.add_more').live('click', function() {
var el_of_form, subtitle, more_subtitles;
el_of_form = $(this).closest('form');
subtitle = $(el_of_form).find('.subtitle');
more_subtitles = $(el_of_form).find('.more_subtitles');
console.log(subtitle);
console.log(more_subtitles);
$(more_subtitles).add(subtitle);
return false;
});
Why isn't it working? console.log()
finds those elements...
What I want to accomplish is that when button is presses, it clones subtitle
element and add it in more_subtitles
element. And user can repeat it again and again.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Add 不是您想要的方法。尝试追加。
Add 用于使用传递给 add 的选择器匹配的一组新元素来扩展当前匹配的元素。它不会影响 DOM。
Append 是实际将匹配的 DOM 元素添加到当前 DOM 元素的方法。
Add is not the method you want. Try append instead.
Add is used to extend the current matched elements with a new set of elements as matched by the selector you passed into add. It does not affect the DOM.
Append is the method that actually will add matched DOM elements to the current DOM element.