1


我的代码采用 TD 标签的标题,以获取字符串中两个时间之间的时间差
(即 13:01-14:03)并将其显示为新标题(属性),这仅适用于只有元素,当我有多个时,它会失败。
我怎样才能做到这一点?

<table>
    <thead>
        <th>First</th>
        <th>Second</th>
        <th>Third</th>
        <th>Ok</th>

    </thead>
    <tr>
        <td>1</td>
        <td>CCBB</td>
        <td class='huifa' id='calc' title='02-01-2013 13:01-14:03'>231</td>
        <td class='huifa' id='calc' title='02-01-2013 13:01-13:53'>1</td>
    </tr>

   <tr>
        <td>2</td>
        <td>CCBB</td>
        <td>342</td>
        <td>0</td>

    </tr>
</table>

JS代码

<script>
element = document.getElementById('calc');
v = element.title
v = v.slice(11)
v = v.split('-')
totalTime = delTime(v[1],v[0]);
element.title = 'Duracion: '+totalTime
</script>

我的工作代码在这里:http: //jsfiddle.net/cespinoza/tg86a/55/

谢谢。

4

1 回答 1

1

首先,将 更改id='calc'class='calc'。然后将您的代码更改为:

elements = document.getElementsByClassName('calc');
for(var i = 0; i < elements.length; i++) {
  v = elements[i].title;
  v = v.slice(11);
  v = v.split('-');
   totalTime = delTime(v[1],v[0]);
   elements[i].title = 'Duration: ' + totalTime;                             
}
于 2013-01-03T22:17:58.370 回答