首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将一个表的值复制到另一个表中(如何将此js函数转换为jQuery)

将一个表的值复制到另一个表中(如何将此js函数转换为jQuery)
EN

Stack Overflow用户
提问于 2010-03-27 03:11:17
回答 2查看 2.7K关注 0票数 1

我遇到了一个小问题..我要做的是将id的描述从一个表复制到另一个表。我已经写了一半的javascript,谁能告诉我如何在jquery中转换这个函数。我希望将基于id的描述从第一个表复制到第二个表。我在jquery中使用了'contains',(Comparing 2 tables column values and copying the next column content to the second table),因为有1000个表行,浏览器崩溃了。有没有办法简化它?...代码如下所示。

当我单击第二个表中的test时,当前的javascript可以工作,但我希望在页面加载时将该值附加到第二个表中……请帮帮忙

代码语言:javascript
复制
<table class="reportTabe">
<tr><td>psx-pdu120v1</td><td class="itemname" id="psx-pdu120v1">some description1</td></tr>
<tr><td>psx-pdu120v1</td><td class="itemname" id="psx-pdu120v1">some description1</td></tr>
<tr><td>psx-pdu120v3</td><td class="itemname" id="psx-pdu120v3">some description3</td></tr>
<tr><td>psx-pdu120v4</td><td class="itemname" id="psx-pdu120v4">some description4</td></tr>
<tr><td>psx-pdu120v5</td><td class="itemname" id="psx-pdu120v5">some description5</td></tr>
<tr><td>psx-pdu120v6</td><td class="itemname" id="psx-pdu120v6">some description6</td></tr>
<tr><td>psx-pdu120v7</td><td class="itemname" id="psx-pdu120v7">some description7</td></tr>
<tr><td>psx-pdu120v8</td><td class="itemname" id="psx-pdu120v8">some description8</td></tr>
<tr><td>psx-pdu120v9</td><td class="itemname" id="psx-pdu120v9">some description9</td></tr>
</table>

<table class="data">
<tr><td class="whipItem">psx-pdu120v1</td><td onClick="Javascript:alert(document.getElementById('psx-pdu120v1').innerText)";>test</td></tr>
<tr><td class="whipItem">psx-pdu120v3</td><td onClick="Javascript:alert(document.getElementById('psx-pdu120v1').innerText)";>test</td></tr>
<tr><td class="whipItem">psx-pdu120v4</td><td onClick="Javascript:alert(document.getElementById('psx-pdu120v5').innerText)";>test</td></tr>
<tr><td class="whipItem">psx-pdu120v5</td><td Javascript:this.innerText=document.getElementById('psx-pdu120v4').innerText;></td></tr>
<tr><td class="whipItem">psx-pdu120v6</td><td Javascript:this.innerText=document.getElementById('psx-pdu120v5').innerText;></td></tr>
<tr><td class="whipItem">psx-pdu120v7</td><td Javascript:this.innerText=document.getElementById('psx-pdu120v6').innerText;></td></tr>
<tr><td class="whipItem">psx-pdu120v8</td><td Javascript:this.innerText=document.getElementById('psx-pdu120v7').innerText;></td></tr>
<tr><td class="whipItem">psx-pdu120v9</td><td Javascript:this.innerText=document.getElementById('psx-pdu120v8').innerText;></td></tr>
</table>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-03-27 03:39:43

代码语言:javascript
复制
$(document).ready(function() {
        $('.whipItem', '.data').each(function(index, element) { //for each whipItem in the data table
            var name = $(element).text(); //get the text value
            var desc = $(".itemname[id='" + name + "']").text(); //get the description whose id matches the name in the report table.
            $(element).next().text(desc); //change the value of the next td to the description.
        });
    });
票数 0
EN

Stack Overflow用户

发布于 2010-03-27 03:19:00

代码语言:javascript
复制
$(function() {

        $.each($('firstTable td'), function(i) {
            var tableData = $(this);
            $('.secondTable td').eq(i).text(tableData.text());
        });
    });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2526003

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档