想要返回包含表格标签的完整html

时间:2010-11-09 14:25:20

标签: javascript jquery

这将返回表中应该包含的所有内容。

alert($('table[width="100%"][cellspacing="0"][cellpadding="10"][border="0"]:eq(0)').html());

如何获取表格内的HTML以及实际的表格标签,在本例中为

<table width="100%" cellspacing="0" cellpadding="10" border="0">
----html----
</table>

3 个答案:

答案 0 :(得分:4)

请参阅this thread

添加此扩展代码:

$.fn.outer = function(val){
    if(val){
        $(val).insertBefore(this);
        $(this).remove();
    }
    else{ return $("<div>").append($(this).clone()).html(); }
}

然后你可以这样做:

alert(
    $('table[width="100%"][cellspacing="0"][cellpadding="10"][border="0"]:eq(0)')
    .outer()
);

答案 1 :(得分:3)

如果它存在,则使用outerHTML属性,否则生成.clone(),将其附加到新的<div>(不在页面上),然后获取.html() div。

var table = $('table[width="100%"][cellspacing="0"][cellpadding="10"][border="0"]:eq(0)');

var outerTag = table[0].outerHTML || $('<div/>').append(table.clone()).html();

alert(outerTag);

答案 2 :(得分:2)

您打算如何处理HTML?

如果你想把它放在DOM的其他地方那么:

$('#place-to-put').append($('table[width="100%"][cellspacing="0"][cellpadding="10"][border="0"]:eq(0)'));