开发者

would like to return the complete html including table tag

开发者 https://www.devze.com 2023-01-24 18:13 出处:网络
This returns everything inside of the table which it should. alert($(\'table[width="100%"][cellspacing="0"][cellpadding="10"][border="0"]:eq(0)\').html());

This returns everything inside of the table which it should.

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

How would I get the HTML inside the table as well as the actual table tag, in this case

<开发者_运维技巧;table width="100%" cellspacing="0" cellpadding="10" border="0">
----html----
</table>


See this thread:

Add this extension code:

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

Then you can do this:

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


This uses the outerHTML property if it exists, otherwise makes a .clone(), appends it to a new <div> (not on the page), and gets the .html() of the 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);


What do you plan to do with the HTML?

If you want to put it somewhere else in the DOM then:

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

精彩评论

暂无评论...
验证码 换一张
取 消