jQuery/JS:复制类的内容并将其打印到页面上的其他地方
jQuery/JS: Copy the contents of a class and print it somewhere else on the page
我有一个名为test-parent
的类,如下所示,我想复制两个UL元素的内容,并将内容放在页面上的其他地方。
<div class="test-parent">
<hr>
<strong>
<ul style="list-style-type:none">
<ul style="list-style-type:none">
</div>
我的jQuery看起来像这样:
(function($) {
$( document ).ready(function() {
$( ".options_group").prepend( " <p>Test</p>" );
});
}(jQuery));
本质上,我所需要做的就是将类中的两个UL元素预先添加到options_group类中。有人知道我该如何做到这一点吗?感谢
尝试一下,只需使用each()和clone函数()即可实现圆顶。正如您所说,您需要在其他地方使用ul元素,或者如果您只需要ul中的内容,则需要相应地修改
(function($) {
$( document ).ready(function() {
//$( ".options_group").prepend( " <p>Test</p>" );
$('.test-parent ul').each(function(){
$( ".options_group").append($(this).clone());
});
});
}(jQuery));
ul{
background:red;
height:100px;
width:100px;
}
.options_group{
clear:both;
border:1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div class="test-parent">
<hr>
<strong>
<ul style="list-style-type:none">1</ul>
<ul style="list-style-type:none">2</ul>
</strong>
</div>
<div class="options_group">
</div>
您可以简单地使用jQuerys克隆函数来克隆uls:
$(function() {
$(".copy").prepend($(".test-parent ul").clone());
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="test-parent">
<hr/>
<ul style="list-style-type:none">
<li>item</li>
</ul>
<ul style="list-style-type:none">
<li>item 2</li>
</ul>
</div>
<div class="copy">
</div>
相关文章:
- 使用javascript将动态表从一个html页面打印到另一个html页
- 如何添加浮动和非浮动,其他
- JavaScript打印功能使日历停止工作
- 与其他库的jQuery.noConflict()
- 播放当前视频时如何停止其他视频?JavaScript
- 剑道UI内联编辑:如何在点击其他按钮时隐藏按钮
- 制作一个不带HTML a标记但在动画播放完毕后指向其他页面的超链接
- esri javascript异步打印
- 除修剪外的其他功能
- 文本框不是从Javascript/Asp.net中的对话框中打印出来的
- 是什么让一个“;Uncaught RangeError:超过了最大调用堆栈大小“;错误(Chrome,在其他浏览器中显示
- 如何将美化类漂亮打印更改为我们喜欢的其他类名
- 有没有其他方法可以在不对 javascript 中的内容进行换行的情况下打印出调试消息
- 在页面加载过程中,在不重定向到其他页面的情况下,在同一页面上打印距离结果
- 当请求php脚本生成的图像时,如何使用javascript/其他方法以某种格式打印日期
- jQuery/JS:复制类的内容并将其打印到页面上的其他地方
- 如何打印字符串数组,这是JSON的一部分,由其他属性组成
- 打印功能后无法点击其他选项卡
- Javascript二维数组在打印其他数组之前给出undefined
- 为什么每次都打印其他报表