使用jQuery包装html的某些部分
Wrapping some part of html using jQuery
我写的是:
<div class="the_notice">
<span class="time"> | 3:48pm</span>
To <strong>2013-03-16_10-56-33</strong> By <strong>XYX</strong>
</div>
在结果中,我想要这样的:
<div class="the_notice">
<span class="time"> | 3:48pm</span>
<div class="wrap">
To <strong>2013-03-16_10-56-33</strong> By <strong>XYX</strong>
</div>
</div>
如何使用jQuery实现这一点?
如果内容总是相同的,你可以这样做
$('.the_notice').contents().slice(2).wrapAll('<div class="wrap"/>');
http://jsfiddle.net/Ysq48/Demo
var temp = $('.time');
$('.time').remove();
$(".the_notice").contents().wrapAll('<div class="wrap" />');
$(".the_notice").prepend(temp);
阅读这个链接,它可能会对你有所帮助:使用jquery创建html元素的最有效方法是什么
我的答案是这样的:
var myTimeIsHere = //calculate your time here;
var x = //calculate x here.;
var y = //calculate y here.;
var newHTML = "<span class='time'>" |+ myTimeIsHere+"</span>"+
"<div class='wrap'>To <strong> "+x+" </strong>"+
"by "+y+"</div>";
$(".the_notice").html(newHTML);
一行(或三行):
$('.the_notice')
.wrapInner($('<div class="wrap">'))
.prepend($('.the_notice .time').detach());
JSFIDDLE
相关文章:
- 可以't让我的if语句处理js中的html表单输入
- 如何设置html元素填充的动画
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- 如何将HTML id分配给元素,以及如何将JavaScript应用于元素
- 如何编写HTML输入的JS内联
- Dojo不解析自定义小部件的模板html中的小部件声明性
- 使用javascript将动态表从一个html页面打印到另一个html页
- 通过javascript重定向html传递php变量
- 如何使Javascript动态html表及其上的事件
- 在chrome.tabs.onCreated之后加载HTML页面
- 如何在vs2002中调试html页面
- 如何使用javascript或html下载PDF格式的填写表单
- 视频HTML没有'无法在Internet Explorer 11上工作
- HTML表单提交时未执行外部函数
- 如何将输入(type=text)从html表单传递到javascript函数
- 使用angular重定向到html页面
- 分析高度属性时出现意外值{{specs.height}}.index.html
- 在html Select中添加搜索
- 多级HTML表单
- 如何将json数据显示为html