English 中文(简体)
Javascript update to HTML table cell (from thousands to millions)
原标题:

I need to create some Javascript on my web report which converts table values to thousands or millions (eg divides or multiplies by 1000).

The trouble is that each value is clickable (i.e. it is wrapped in an anchor tag).

Is this possible?

<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>

edit

I am using IE6. basically the aim is to have a thousands and a millions button so that the values of the table can become 11,372k or 11.4m etc...

最佳回答

I would use jQuery

$(function (){
    $.each($( .nums ), function () {
        $(this).html(Number($(this).html().replace(/,/g,"")) /*your math here*/);
    });
});

In response to your edit: Since I m bored I wrote this piece of working code for you, round and insert , as you see fit.

<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>
问题回答

I would start thinking about document.getElementsByClassName( nums ), which will give you an array of your elements. Something like this:

nums = document.getElementsByClassName( nums );
for (i in nums)
   nums[i].innerHTML = nums[i]+ ,000 ;i

This likely only works in newer browsers, though, so you may want to use a similar method in a javascript framework. Jquery allows getting elements by classname with the selector

$( .nums )




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签