简体   繁体   English

使用jQuery从HTML删除表格

[英]Remove tables from HTML using jQuery

I've got many chunks of HTML coming into my app which I have no control over. 我的应用程序中有很多HTML代码,我无法控制。 They contain tables for layout, which I want to get rid of. 它们包含用于布局的表,我想摆脱这些表。 They can be complex and nested. 它们可以是复杂的和嵌套的。

What I want is to basically extract the HTML content from the tables, so that I can inject that into other templates in the app. 我想要的基本上是从表中提取HTML内容,以便可以将其注入到应用程序的其他模板中。

I can use jQuery or plain JS only, no server side trickery. 我只能使用jQuery或纯JS,没有服务器端的麻烦。

Does anyone know of a jQuery plugin of good tip that will do the job? 有谁知道一个很好的jQuery插件来完成这项工作?

Littm - I mean to extract content from the td tags essentially. Littm-我的意思是本质上从td标签提取内容。 I want no trace of table left in the code when it's done. 完成后,我不希望在代码中留下任何表的痕迹。 Thanks! 谢谢!

Here's an example of the type of HTML in question. 这是有关HTML类型的示例。 It all comes in via XHR from some ancient application so I have no control over the markup. 所有这些都是通过XHR从某个古老的应用程序中获得的,因此我无法控制标记。 What I want is to get rid of the tables completely, leaving just the rest of the HTML or other text content. 我想要的是完全摆脱表格,只剩下HTML或其他文本内容的其余部分。

<table>
    <tr><td colspan="4"></td></tr>
    <tr>
        <td width="1%"></td>
        <td width="40%" style="padding-left: 15px">
        <p>Your access level:  <span>Total</span></p>
        </td>
        <td width="5%">
            <table><tr><td><b>Please note</b></td></tr></table>
        </td>
        <td width="45%" style="padding-left: 6px" valign="top"><p>your account</p></td>
    </tr>
    <tr><td colspan="4">&nbsp;</td></tr>
    <tr>
        <td width="1%"></td>
        <td width="40%" style="padding-left: 15px">
            <table>
                <tr>
                    <td align="right">Sort Code:  </td>
                    <td align="center"><strong>22-98-21</strong></td>
                </tr>
                <tr>
                    <td align="right">Account Number:  </td>
                    <td><strong>1234959</strong></td>
                </tr>
            </table>
        </td>
        <td width="5%"></td>
        <td width="45%" style="padding-left: 6px">Your account details for</td>
    </tr>
</table>

I've tried; 我试过了;

var data = ""
$("td").each(function(){
  data += $(this).html()
});
$("article").append(data);
$("table").remove();

But var data still contains nested td tags. 但是var data仍然包含嵌套的td标签。 I'm no JS expert so I'm not sure what else to try.... 我不是JS专家,所以我不确定还有什么尝试...。

Let's suppose that you have X number of tables. 假设您有X个表。

In order to extract all the content information from these, you could try something like this: 为了从中提取所有内容信息,您可以尝试执行以下操作:

// Array containing all the tables' contents
var content = [];

// For each table...
$("table").each(function() {

    // Variable for a table
    var tb = [];
    $(this).find('tr').each(function() {

        // Variable for a row
        var tr = [];

        $(this).find('td').each(function() {
            // We push <td> 's content
            tr.push($(this).html());
        });

        // We push the row's content            
        tb.push(tr);
    });
    // We push the table's content
    content.push(tb);
});

So for instance, if we have the following 2 tables: 因此,例如,如果我们有以下两个表:

<table>
    <tr>
        <td>1</td>
        <td>2</td>
    </tr>
    <tr>
        <td>A</td>
    </tr>
</table>

<table>
    <tr>
        <td>r1</td>
        <td>r2</td>
        <td>r3</td>
    </tr>
    <tr>
        <td>rA</td>
        <td>rB</td>
    </tr>
    <tr>
        <td>rP</td>
    </tr>
</table>

The array content will be something like this: 数组content将如下所示:

content = [ [ [1, 2], [A] ] ] , [ [r1, r2, r3], [rA, rB], [rP] ] ]
            \_______________/   \______________________________/
                 1st table                2nd table

and if you want to access the first table, you'll just have to access content[0] for instance. 如果要访问第一个表,则只需要访问content[0]


Now, let's suppose that you have a DIV, with and id my_div , and that you want to output some table content in it. 现在,假设您有一个DIV,其ID为my_div ,并且要在其中输出一些表内容。

For example, let's suppose that you only want to have the 1st table only. 例如,假设您只想拥有第一个表。 Then, you would do something like this: 然后,您将执行以下操作:

// Note: content[0] = [[1, 2], [A]]

var to_print = "<table>";

for(var i=0; i<content[0].length; i++) {
    to_print += "<tr>";     
    for(var j=0; j<content[0][i].length; j++)       
        to_print += "<td>"+ content[0][i][j] +"</td>";              

    to_print += "</tr>";        
}

to_print += "</table>";

$("#my_div").html(to_print);

which will give you something like this: 这会给你这样的东西:

<div id="my_div">
    <table>
        <tr>
            <td>1</td>
            <td>2</td>
        </tr>
        <tr>
            <td>A</td>
        </tr>
    </table>
</div>

Hope this helps. 希望这可以帮助。

Edit: You should create a recursive function to do that. 编辑:您应该创建一个递归函数来做到这一点。

Simply create you function that gets "td"'s as a value: 只需创建将“ td”作为值的函数:

function searchTexts(td) {
     if (td.find("td")) {
           searchTexts(td.find("td"));
     }
     td.each(function(){
         data += $(this).html()
         $(this).remove(); // remove it for avoiding duplicates
     });
}

Then call it passing a jQuery object to the function. 然后调用它,将jQuery对象传递给函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM