我需要在网络报告中创建一些Javascript,将表值转换为数千或数百万(例如,除以1000或乘以1000)。
问题在于每个值都是“可点击的”(即,它包装在锚定标记中)。
这可能吗?
<table class="Table" >
<thead><tr>
<th class="l Header" scope="col">£000s</th>
<th class="l Header" scope="col">Col1</th>
<th class="l Header" scope="col">Col2</th>
</tr></thead>
<tbody>
<tr>
<td class="l Data"> My Data Row </td>
<td class="l Data"> <a class=nums href="javascript:MyFunc();"> 11,372,397</a></td>
<td class="l Data"> <a class=nums href="javascript:MyFunc();"> 11,344,327</a></td>
</tr>
</tbody>
编辑
我正在使用IE6。基本上目的是要有一个“千”和“百万”按钮,以便表的值可以变成11,372k或11.4m等。
最佳答案
我会用jQuery
$(function (){
$.each($('.nums'), function () {
$(this).html(Number($(this).html().replace(/,/g,"")) /*your math here*/);
});
});
回应您的编辑:
由于我很无聊,所以我为您编写了这段工作代码,并在您认为合适的情况下四舍五入并插入“”。
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<table class="Table" >
<thead><tr>
<th class="l Header" scope="col">£000s</th>
<th class="l Header" scope="col">Col1</th>
<th class="l Header" scope="col">Col2</th>
</tr></thead>
<tbody>
<tr>
<td class="l Data"> My Data Row </td>
<td class="l Data"> <a class="nums" href="javascript:MyFunc();"> 11,372,397</a></td>
<td class="l Data"> <a class="nums" href="javascript:MyFunc();"> 11,344,327</a></td>
</tr>
</tbody>
<input type="button" onclick="toMillions();">
<input type="button" onclick="restore();">
<script type="text/javascript">
$(function (){
$.each($('.nums'), function () {
$.data(this, "theValue", $(this).html());
});
});
function toMillions(){
$.each($('.nums'), function () {
$(this).html(Number($.data(this,"theValue").replace(/,/g,"")) / 1000000 + ' million');
});
}
function restore(){
$.each($('.nums'), function () {
$(this).html($.data(this,"theValue"));
});
}
</script>