简体   繁体   English

jQuery替换表中的内容

[英]jQuery replace content in a table

I have a table that is rendered to the page that contains a link to a website. 我有一个表格,呈现给包含网站链接的页面。 This allows for the user to easily click on it and navigate etc. 这允许用户轻松点击它并导航等。

I am building a function that I can pass the table to and it will export the data to excel. 我正在构建一个可以将表传递给它的函数,它将数据导出到excel。

The first part of this though is to create a variable of this table data and remove the link from the column leaving it with only the text. 这部分的第一部分是创建此表数据的变量,并从列中删除链接,只留下文本。

Here is my table: 这是我的表:

<table id="assignedOpenProjects">
<thead>
    <tr>
        <th>Col 1</th>
        <th>Col 2</th>
        <th>Col 3</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td>Bob</td>
        <td>1234</td>
        <td><a href="http://www.google.com">Link</a></td>
    </tr>
</tbody>
</table>

The 3rd column in the table is the one that contains the link (2nd in terms of 0 based). 表中的第3列是包含链接的列(基于0的第2列)。

My jQuery: 我的jQuery:

// Define the HTML of the table
var table = $('#assignedOpenProjects').prop('outerHTML');

// Loop over all of the table rows
 $(table).find('tbody > tr').each(function () {

    // Loop over the column that contains the link
     $(this).children("td").eq(2).each(function () {

         // Define the value of that column
         var col = $(this).html();

         // Replace the value with the text version
         $(col).replaceWith($(col).text());

     });

});
// Output the table
$('#output').empty().append(table);

When I run this, the content is identical and it doesn't remove the link. 当我运行它时,内容是相同的,它不会删除链接。 However if I log the $(col).text() to the console, it shows what I am expecting the column to be replace with. 但是,如果我将$(col).text()到控制台,它会显示我期望要替换的列。

Can some one tell me what I am doing wrong as to why this content is not being replaced as I would expect? 有人可以告诉我,为什么这些内容没有像我期望的那样被替换,我做错了什么?

The expected outcome should just contain the word Link in Col3 once in the output. 预期结果应该只在输出中包含一次Col3中的单词Link

JS Fiddle: https://jsfiddle.net/zrpe8c3x/2/ JS小提琴: https//jsfiddle.net/zrpe8c3x/2/

The approach you're taking of retrieving the HTML of the table and hacking it around is massively overcomplicated. 您正在采取的检索表格的HTML并将其黑客攻击的方法过于复杂。 From your description, all you're trying to do is remove the link functionality from the a elements, but keep the text. 从你的描述,你正在试图做的一切是从删除的链接功能a元素,但保留文本。 If this is the case, you can make it a simple one-liner using unwrap() : 如果是这种情况,您可以使用unwrap()将其unwrap()简单的单行程序:

$('#assignedOpenProjects').find('a').contents().unwrap();

Updated fiddle 更新了小提琴

Your code is not working because you initialize your table variable using the outerHTML , but you don't modify it afterwards, so what you're appending at the end of your code is the same variable you defined before, unchanged, which is the same table you had as an input 您的代码无效,因为您使用outerHTML初始化table变量,但之后不会对其进行修改,因此您在代码末尾添加的内容与之前定义的变量相同,不变,这是相同的你有一个表作为输入

It happens because you have the following problems in your code: 这是因为您的代码中存在以下问题:

  1. The table variable you're appending at the end is the plain outerHTML and not the jQuery object you build and traverse 您最后附加的table变量是plain outerHTML而不是您构建和遍历的jQuery对象

  2. You're changing a new td object inside, which never gets appended back to the original td , so you end up changing something that you don't use 你正在更改一个新的td对象,它永远不会被附加到原始的td ,所以你最终改变你不使用的东西

Here is your code with both things corrected: 这是你的代码纠正了两件事:

$('[name=clean]').click(function(){

    // Define the HTML of the table
    var table = $('#assignedOpenProjects').prop('outerHTML');

    var table2 = $(table);
    // Loop over all of the table rows
     table2.find('tbody > tr').each(function () {

        // Loop over the column that contains the link
         $(this).children("td").eq(2).each(function () {

             // Define the value of that column
             var col = $(this);

             // Replace the value with the text version
             $(col).replaceWith($(col).text());

         });

    });
    // Output the table
    $('#output').empty().append(table2);
});

https://jsfiddle.net/qs3mk7xn/ https://jsfiddle.net/qs3mk7xn/

That being said, I believe you should pay attention to what Rory answered. 话虽如此,我相信你应该注意罗瑞的回答。 If you want the same thing he did but in a different table, you could do something like this: 如果你想要他做同样的事情,但在另一张桌子上,你可以这样做:

$('[name=clean]').click(function(){
    $('#output').empty().append($('#assignedOpenProjects').clone().find('a').contents().unwrap().parents().last());
});

https://jsfiddle.net/qs3mk7xn/1/ https://jsfiddle.net/qs3mk7xn/1/

Or, more efficiently, something like this: 或者,更有效率,这样的事情:

$('[name=clean]').click(function(){
    var clon = $('#assignedOpenProjects').clone();
    clon.find('a').contents().unwrap();
    $('#output').empty().append(clon);
});

https://jsfiddle.net/qs3mk7xn/2/ https://jsfiddle.net/qs3mk7xn/2/

Or, if you want the third td only, something like this: 或者,如果你只想要第三个td ,就像这样:

$('[name=clean]').click(function(){
    var clon = $('#assignedOpenProjects').clone();
    clon.find('tbody tr td:nth-child(3) a').contents().unwrap();
    $('#output').empty().append(clon);
});

https://jsfiddle.net/qs3mk7xn/3/ https://jsfiddle.net/qs3mk7xn/3/

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

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