1



My Code は、文字列内の 2 つの時間(つまり、13:01-14:03)の時間差を取得するために TD タグのタイトルを取得し、新しいタイトル (プロパティ) のように表示します。これは、要素のみ、複数ある場合は失敗します。
この場合、どうすればうまくいくでしょうか??

<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 に答える