通过jquery删除TABLE,TR,TD标签

时间:2013-04-14 17:22:50

标签: jquery

是否可以通过jquery删除“TABLE,TR,TD”标签,但不应在表格内删除内容。

即。下面是我的表格代码包含图像,我想删除图像周围的table,tbody,tr和td标签。

jquery之前的代码

<div id="Gallery">
    <table>
      <tbody>
        <tr>
            <td><img src="image1.jpg"></td>
        </tr>
        <tr>
            <td><img src="image2.jpg"></td>
        </tr>
        <tr>
            <td><img src="image3.jpg"></td>
        </tr>
      </tbody>
    </table>
</div>

代码应该在jquery之后

<div id="Gallery">
    <img src="image1.jpg">
    <img src="image2.jpg">
    <img src="image3.jpg">
</div>

提前致谢!

8 个答案:

答案 0 :(得分:3)

尝试:

var gallery = $("#Gallery");
var contents = gallery.find("td, th").contents().detach();
gallery.find("table").remove();
gallery.append(contents);

DEMO: http://jsfiddle.net/Ydsg9/

答案 1 :(得分:2)

$('#Gallery table').replaceWith(function(){
     return $(this).find('td *');
});

http://jsfiddle.net/ec46T/

答案 2 :(得分:2)

$("#Gallery img").each(function(){
    $("#Gallery").append($(this));
});
$("#Gallery table").remove();

http://jsfiddle.net/hescano/jGT5b/

答案 3 :(得分:0)

Working Fiddle

var img = "";
$("td").each(function(){
  img = img + $(this).html();
});

$("#Gallery").append(img);
$("table").remove();

答案 4 :(得分:0)

试试这个:

$('#Gallery img').unwrap().unwrap().unwrap().unwrap();

答案 5 :(得分:0)

如果您只想要<td>元素内的内容,则可以使用以下内容:

 $(function(){
    var g = $('#Gallery'),
    f = '';

    $('td', g).each(function(){
        f += $(this).html();
    });

    g.html(f);

});

答案 6 :(得分:0)

另一种方法

        var gallery = $("#Gallery");
        var imgs = $("img", gallery).clone();

        $("table", gallery).remove();
        gallery.append(imgs);

选择器已优化,imgs变量仍然引用#Gallery中插入的图像

答案 7 :(得分:0)

这是

var list = $('#Gallery table td *');
$('#Gallery').html(list.get());

您可以在此处使用它:http://jsfiddle.net/cockypup/Dehgf/2/

CSS只是表明桌子正在消失。