2013-03-14 141 views
1

我有以下表结构:访问标记通过JavaScript

<table id="currentloc_table"> 
    <tr> 
    <th></th> 
    <th>Name</th> 
    <th>Details</th> 
    </tr> 
    <tr> 
     <td><input id='viewonMapCheckbox' type='checkbox'></td> 
     <td> 
      <span class="tooltip" href="#"> 
       <span class="custom info"> 
        <em> 
        </em> 
        Last Activity: 2013-03-12 03:29:21<br/> 
        Latitude: <span id="lat">30</span><br/> 
        Longitude: <span id="lon">70</span><br/> 
       </span> 
      </span> 
     </td> 
     <td>Details</td> 
    </tr> 
</table> 

我需要使用JavaScript设置纬度和经度值,但不能使它工作。请帮助我。这是我写的代码:

var loc_table = document.getElementById('currentloc_table'); 
var loc_table_rows_count = document.getElementById('currentloc_table').getElementsByTagName("tr").length; 
for (var i = 0; i < loc_table_rows_count; i++) { 
    if (i > 0) {//skipping header row 
     var row = loc_table.rows[i]; 
     row.cells[1].firstChild.firstChild.getElementById("lat").innerHTML = "31"; 
     row.cells[1].firstChild.firstChild.getElementById("lon").innerHTML = "71"; 
    } 
} 
+0

你为什么要使用循环,如果你有单列? – Cris 2013-03-14 13:08:00

+0

@cris可以有多行。我将单行显示为示例数据。 – Azeem 2013-03-14 13:16:55

+0

然后每个纬度和经度都会有相同的ID,请确保您有不同的ID或通过名称 – Cris 2013-03-14 13:29:28

回答

2

id是一个全球性的领域,只是这样做:的

document.getElementById("lat").innerHTML = "31"; 
document.getElementById("lon").innerHTML = "71"; 

什么,而不是你写。

+0

访问它不起作用。跨文本没有得到更新。 – Azeem 2013-03-14 13:13:00

+0

你什么时候调用javascript代码?您不能只在一开始就分配这些值,只有在页面完全加载时才应该运行此JavaScript代码。看看body onload事件,以便能够在你的页面加载时运行:http://www.w3schools.com/jsref/event_body_onload.asp – aram90 2013-03-14 13:15:23

+0

实际上,我已经在按照你的建议进行操作了。这段代码是在页面加载时触发的JS函数中写入的。 – Azeem 2013-03-14 13:17:58

0
var loc_table = document.getElementById('currentloc_table'); 
var loc_table_rows_count = document.getElementById('currentloc_table').getElementsByTagName("tr").length; 
for (var i = 1; i < loc_table_rows_count; i++) { 
     document.getElementById("lat"+i).innerHTML = "31"; 
     document.getElementById("lon"+i).innerHTML = "71"; 
} 

而在你的HTML,添加在id属性的行数。 例如:

<table id="currentloc_table"> 
    <tr> 
    <th></th> 
    <th>Name</th> 
    <th>Details</th> 
    </tr> 
    <tr> 
     <td><input id='viewonMapCheckbox' type='checkbox'></td> 
     <td> 
      <span class="tooltip" href="#"> 
       <span class="custom info"> 
        <em> 
        </em> 
        Last Activity: 2013-03-12 03:29:21<br/> 
        Latitude: <span id="lat1">30</span><br/> 
        Longitude: <span id="lon1">70</span><br/> 
       </span> 
      </span> 
     </td> 
     <td>Details</td> 
    </tr> 
</table> 
+0

其不工作... – Azeem 2013-03-14 13:18:41

+0

附加什么?给更多的解释...你是否在span中添加一个行索引,以确保**所有id **都是**唯一的** – JoDev 2013-03-14 13:27:22

0

看起来你需要有一排以上。在这种情况下,你不能使用ID作为跨度(我们应该在页面上有一个唯一的ID)。 关于解决您的任务: 将您的ID属性更改为CLASS。 简单的JS解决方案:

var loc_table = document.getElementById('currentloc_table'); 
var loc_table_rows = loc_table.getElementsByClassName("table_row"); 
for (var i = 0; i < loc_table_rows.length; i++) { 
    var row = loc_table_rows[i]; 
    var rowLatitude=row.getElementsByClassName('lat')[0].innerHTML='31'; 
    var rowLongitude=row.getElementsByClassName('lon')[0].innerHTML='71'; 
} 

HTML例如:

<table id="currentloc_table"> 
    <tr> 
     <th></th> 
     <th>Name</th> 
     <th>Details</th> 
    </tr> 
    <tr class="table_row"> 
     <td> 
      Latitude: <span class="lat">30</span><br/> 
      Longitude: <span class="lon">70</span><br/> 
     </td> 
    </tr> 
    <tr class="table_row"> 
     <td> 
      Latitude: <span class="lat">30</span><br/> 
      Longitude: <span class="lon">70</span><br/> 
     </td> 
    </tr> 
</table> 
+0

可以请你推荐Javascript代码来实现这个,而不是通过jquery。谢谢 – Azeem 2013-03-14 13:20:14

+0

http://jsfiddle.net/DzVaH/ – 2013-03-14 14:22:56