通过jQuery删除TABLE,TR,TD标签
是否可以通过jquery删除“TABLE,TR,TD”标签,但不应删除表内的内容。通过jQuery删除TABLE,TR,TD标签
即下面是我的表代码包含图像,我想删除图像周围的表,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>
谢谢!
var img = "";
$("td").each(function(){
img = img + $(this).html();
});
$("#Gallery").append(img);
$("table").remove();
试试这个:
$('#Gallery img').unwrap().unwrap().unwrap().unwrap();
有趣的aproach,虽然不像其他结构灵活。 –
$('#Gallery table').replaceWith(function(){
return $(this).find('td *');
});
尝试:
var gallery = $("#Gallery");
var contents = gallery.find("td, th").contents().detach();
gallery.find("table").remove();
gallery.append(contents);
好的使用'.detach()'! –
DEMO UPDATED:http://jsfiddle.net/Ydsg9/36/ – sqlchild
如果你只期待<td>
元素中的内容,你应该是,你可以使用以下命令:
$(function(){
var g = $('#Gallery'),
f = '';
$('td', g).each(function(){
f += $(this).html();
});
g.html(f);
});
$("#Gallery img").each(function(){
$("#Gallery").append($(this));
});
$("#Gallery table").remove();
的另一种方式这样做
var gallery = $("#Gallery");
var imgs = $("img", gallery).clone();
$("table", gallery).remove();
gallery.append(imgs);
选择进行了优化和imgs
变量还是要插在#Gallery
这里的图像是
var list = $('#Gallery table td *');
$('#Gallery').html(list.get());
您可以在这里发挥它的引用:http://jsfiddle.net/cockypup/Dehgf/2/
的CSS只是为了证明表格正在消失。
您在'each(function()'之后缺少一个'{'',如果纠正了它,则会在图像前面添加'undefined' – Ejaz
谢谢@Ejay ..我更正了问题 –
使用'.html()' jQuery事件处理程序和数据丢失 – Ian