简体   繁体   English

使用Javascript将单元格数据从一个表拉到另一个表

[英]Using Javascript to pull cell data from one table to another

I don't know if this is possible, and I've been digging around for code for hours with no luck, so I'm going to give this a shot. 我不知道这是否可行,而且我一直在努力寻找代码,但运气不佳,所以我将试一试。

Let's say I have a table on a page. 假设我在页面上有一张桌子。 It contains the following: 它包含以下内容:

<table id="srctb">
    <tr>
        <td>Users</td>
        <td>Musers</td>
        <td>Abusers</td>
    </tr>
    <tr>
        <td>50</td>
        <td>34</td>
        <td>16</td>
    </tr>
</table>

On a different page, I have another table. 在另一个页面上,我还有另一个表。 It looks like this: 看起来像这样:

<table id="desttb">
    <tr>
        <td>Users</td>
        <td>Musers</td>
        <td>Abusers</td>
    </tr>
    <tr>
        <td> </td>
        <td> </td>
        <td> </td>
    </tr>
</table>

Without having to manually enter the values, like they are in the srctb table, I want to populate table desttb with the exact same values. 无需像srctb表中那样手动输入值,我想用完全相同的值填充表desttb。

Is there a piece of javascript code that will fetch the value of a cell in table srctb, and stick it into a cell in table desttb? 是否有一段JavaScript代码可以获取表srctb中单元格的值,并将其粘贴到表desttb中的单元格中?

Any help is greatly appreciated! 任何帮助是极大的赞赏!

EDIT: If there isn't a way to do it with javascript, I do believe I can use jquery to a certain extent, but I'm not sure. 编辑:如果没有办法用javascript做到这一点,我相信我可以在一定程度上使用jquery,但是我不确定。 I know there are better ways to do this, but unfortunately the site where this needs to work is rather limited in capability. 我知道有更好的方法可以做到这一点,但是不幸的是,需要工作的站点功能有限。

You could save the content of the table in a localstorage object. 您可以将表的内容保存在localstorage对象中。

page 1: 第1页:

window.onbeforeunload = function() { // when you switch page
  localStorage.setItem("content", document.getElementById("from").innerHTML); // save the data to localstorage
};

page 2: 第2页:

window.onload = function () {
  var content = localStorage.content;
  if (content !== undefined) {
    document.getElementById("to").innerHTML = content;
  }
}

I hope this works! 我希望这行得通!

code example 代码示例

Assuming you can use jQuery 1.8 or later, you should be able to do something like this: 假设您可以使用jQuery 1.8或更高版本,则应该能够执行以下操作:

$.get('/srctab.html').done(function(data) {
  $.parseHTML(data).find('#srctb > *').appendTo('#desttb');
});

This grabs the other page using AJAX, parses it as HTML, finds descendants of #srctb, and appends them to #desttb. 这将使用AJAX捕获另一个页面,将其解析为HTML,找到#srctb的后代,并将其附加到#desttb。

I haven't tested this yet, and it may run afoul of the idea that each table has an implicit or explicit tbody that contains the rows. 我尚未对此进行测试,并且可能与每个表都有一个包含行的隐式或显式tbody的想法产生冲突。

Also, if you need the actual data values instead of the whole row, you'll have to use a few more complicated selectors to get them, but the basic idea of grabbing the page and parsing it still applies. 另外,如果您需要实际的数据值而不是整个行,则必须使用一些更复杂的选择器来获取它们,但是获取页面并对其进行解析的基本思想仍然适用。

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

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