我有一个表格,其中包含依赖于上一页的值 这些线索显然是用支柱填充的
但是我对所有这些东西都不是很好,所以有一个简单的问题,我可以做这样的事情,或者只使用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解决方案可能吗?
答案 0 :(得分:2)
鉴于确切的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>
答案 1 :(得分:1)
假设您想将值'groupID'(在第一个tr的td中设置)放入第二行的td元素中,那么以下jQuery将会起到作用:
$(document).ready(function() {
// Scrape the XML out of the TD containing the xml
var tdXml = $('table tr').eq(0).find('td').html();
// Grab the value of the value attribute (groupID)
var value = $(tdXml).attr('value');
// Set this value in the second row's TD
$('table tr').eq(1).find('td').text(value);
});