javascript - 从以前的 TD 设置 TD 值

标签 javascript html css struts2

我有一个表格,其中填充了依赖于前一页的值 那些 avlues 显然充满了支柱

但我不是很擅长所有这些东西,所以有没有一个简单的方法可以做这样的事情,要么只使用 HTML,要么我可以在页面中放入一个小的 javascript?

<Table>
<TR>
<TH>ID</TH>
<TD><s:property value="groupID" /><TD> <--Number generated by the page with struts
</TR>
<TR>
<TH>ID Value</TH>
<TD>foobar</TD> <-- the text I want to add in dependant on the number from the previous TD
</TH>
</Table>

那么,有没有一种简单的方法可以做到这一点?也许是 HTML、CSS、JavaScript 解决方案?

最佳答案

给定确切的 HTML,一个像这样的简单脚本就可以做到。但是,如果您不了解 JavaScript,则应该真正学习它,以便了解自己在做什么。

<body>
    <Table>
        <TR>
            <TH>ID</TH>
            <TD><s:property value="groupID" /><TD>
        </TR>
        <TR>
            <TH>ID Value</TH>
            <TD>foobar</TD>
        </TR>
    </Table>
       <!-- The script is here so it won't run until the TABLE has loaded -->
    <script type="text/javascript">

          // This is a map of the struts values to your values
        var valueMap = {
            "1": "foo",
            "2": "bar",
            "3": "baz"
        };
          // Browser compatibility. Older IE uses 'innerText' instead of 'textContent'
        var textContent = ('textContent' in document) ? 'textContent' : 'innerText';

          // Get all the TD elements on the page
        var tds = document.getElementsByTagName('td');

          // Get the text content of the first TD (index 0)
        var text = tds[0][textContent];

          // Get the value from the map using the text we fetched above
        var value = valueMap[text];

          // Set the text content of the second TD (index 1) to the mapped value
        tds[1][textContent] = value;
    </script>
</body>

关于javascript - 从以前的 TD 设置 TD 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13071733/

相关文章:

javascript - 通过串联引用 javascript 变量

javascript - 从字符串序列中获取 n 对

html - 完全不可见的 html 按钮

html - 将文本环绕在圆形 div 周围

html - 右对齐 Bootstrap 导航栏支持的内容

css - ul 不环绕图像

javascript - JavaScript 和 Node.js 中多个请求之间的局部变量

javascript - 通过 Selenium 和 Java 使用 IEDriverServer 和 Internet Explorer 的 Microsoft Dynamics CRM 中的“XPathEvaluator 未定义”脚本错误

javascript - 无法使复选框成为必填项

javascript - 当我打开下一个项目时,如何使上面的树列表项目自动关闭?