jQuery:一个简单的标签替换 [英] jQuery : A simple tag replacement

查看:78
本文介绍了jQuery:一个简单的标签替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想用<tr>

function test() {
$("p").each(function(index) {
var htmlStr = $(this).html();
$(this).replaceWith("<tr>" + htmlStr + "<\/tr>");
});
}

它不起作用.替换确实可以,但是<p>标记的html内容已删除.因此输出将类似于<tr></tr>

It's not working. the replacement does work but the html content of the <p> tags is removed. so the output would be something like <tr></tr>

此外,此功能将遍历整个html页面,对吗?我只希望在我的#content_div上对其进行处理.我应该添加什么还是在$("p")之前?

Also, this function will go through all the entire html page, right ? I just want it to be processed on my #content_div. Is there anything I should add or before $("p") ?

推荐答案

这是因为<TR>不能包含<TH><TD>以外的任何内容. jsFiddle

That's because <TR> can't contain anything other than <TH> or <TD>. jsFiddle

function test() {
    $("#content_div").find('p').each(function(index) {
        var htmlStr = $(this).html();
        $(this).replaceWith("<tr><td>" + htmlStr + "</td><\/tr>");
    });
}

这篇关于jQuery:一个简单的标签替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆