简体   繁体   English

通过Javascript访问HTML表格中的标签

[英]Access Tags inside a HTML table through Javascript

I have following table structure: 我有以下表结构:

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

I need to set Latitude and Longitude values using Javascript but not able to make it work. 我需要使用Javascript设置纬度和经度值,但无法使其工作。 Please help me out. 请帮帮我。 Here is the code which i have written: 这是我写的代码:

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";
   }
}

id is a global field, just do this: id是一个全局字段,只需这样做:

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

instead of what you have written. 而不是你写的。

Looks like you need to have more than one row. 看起来你需要有多行。 In this case you can't use ID's for spans (we should have one unique ID on page). 在这种情况下,您不能使用ID作为跨度(我们应该在页面上有一个唯一的ID)。 About resolving your task: Change your ID attribute to CLASS . 关于解决您的任务:将您的ID属性更改为CLASS Simple JS solution: 简单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 example: 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>
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";
}

And in your HTML, add the number of the row in the id attribute. 在HTML中,添加id属性中的行号。 For example : 例如 :

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

何从 html 表标签内部访问数据<div id="text_translate"><p>我正在制作一个 HTML 表格并在表格内使用表格。 数据在表格内完美显示,但我想从表格中访问一些数据并将其显示在表格标签之外,我该怎么做? 非常感谢您的帮助</p><p>页面上的数据显示如下:</p><p> <a href="https://i.stack.imgur.com/CytCE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CytCE.png" alt="这就是桌子的样子"></a></p><p> 但我想要这样的东西:访问边表标签中的一列<a href="https://i.stack.imgur.com/VMST5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VMST5.png" alt="像这样"></a></p><pre> &lt;:DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;.-- CSS only --&gt; &lt;link rel="stylesheet" href="https.//stackpath.bootstrapcdn.com/bootstrap/4.5.1/css/bootstrap,min.css" integrity="sha384-VCmXjywReHh4PwowAiWNagnWcLhlEJLA5buUprzK8rxFgeH0kww/aWY76TfkUoSX" crossorigin="anonymous"&gt; &lt;,-- JS: Popper.js. and jQuery --&gt; &lt;script src="https.//code.jquery.com/jquery-3.5.1:slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"&gt;&lt;/script&gt; &lt;script src="https.//cdn.jsdelivr.net/npm/popper.js@1.16:1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"&gt;&lt;/script&gt; &lt;script src="https.//stackpath.bootstrapcdn.com/bootstrap/4.5.1/js/bootstrap:min;js" integrity="sha384-XEerZL0cuoUbHE4nZReLT7nx9gQrQreJekYhJD9WNWhH8nEW+0c5qq7aIo2Wl30J" crossorigin="anonymous"&gt;&lt;/script&gt; &lt;/head&gt; &lt;style&gt;:new { float;right: width;98%: margin-top;0: margin-right;20px: margin-bottom;0. margin-left:auto; }:descp{ background-color;lightgreen: font-size; 12px: font-weight: normal: } &lt;/style&gt; &lt;br&gt; &lt;div class= "head new"&gt; &lt;center&gt; &lt;h1&gt; PROMOTION PROPOSAL &lt;/h1&gt; &lt;/center&gt; &lt;/div&gt; &lt;div id="container" class="new"&gt; &lt;table table id="example" class="table table-bordered table-striped " style="width:100%" &gt; &lt;col style="width:14%"&gt; &lt;col style="width:9%"&gt; &lt;col style="width:15%"&gt; &lt;col style="width:8%"&gt; &lt;col style="width:6%"&gt; &lt;col style="width:6%"&gt; &lt;col style="width;6%"&gt; &lt;col style="width:6%"&gt; &lt;thead&gt; &lt;tr&gt; &lt;th colspan="5"&gt;&lt;/th&gt; &lt;th style="background-color;powderblue:text-align;center:"colspan="2"&gt;OFF INVOICE&lt;/th&gt; &lt;th style="background-color;powderblue.text-align.center."colspan="3"&gt;RETROSPECTIVE&lt;/th&gt; &lt;th colspan="2"&gt;&lt;/th&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th&gt;Product Code&lt;/th&gt; &lt;th&gt;Outer Barcode&lt;/th&gt; &lt;th&gt;Product Description&lt;/th&gt; &lt;th&gt;Pack Size &amp; Weight&lt;/th&gt; &lt;th&gt;Standard Invoice Cost of Product&lt;/th&gt; &lt;th&gt;Bonus&lt;/th&gt; &lt;th&gt;Net Cost&lt;/th&gt; &lt;th&gt;Retro Bonus&lt;/th&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th class="descp" colspan="1"&gt;Enterde in this column&lt;/th&gt; &lt;th class="descp" colspan="1"&gt;Enter your OUTER barcode in this column&lt;/th&gt; &lt;th class="descp" colspan="1"&gt;Enter the full product name with how you would like&lt;/th&gt; &lt;th class="descp" colspan="1"&gt;Pack size &amp; weight in this box or numb&lt;/th&gt; &lt;th class="descp"colspan="1"&gt;Standard invoice cost of product&lt;/th&gt; &lt;th class="descp" colspan="1"&gt;Enter here how&lt;/th&gt; &lt;th class="descp" colspan="1"&gt;Net&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;&lt;input type="text" class="form-control" name="your_name[0]" placeholder="Off Invoice example -CRG123" required&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" class="form-control" name="your_name[0]" placeholder="50212341234" required&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" class="form-control" name="your_name[0]" placeholder="Brand Name Crunchy Cornflakes" required&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" class="form-control" name="your_name[0]" placeholder="£25 x 250g" required&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" class="form-control" name="your_name[0]" placeholder="£3.05" required&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" class="form-control" name="your_name[0]" placeholder="£0.5" required&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" class="form-control" name="your_name[0]" placeholder="£2.55" required&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" class="form-control" name="your_name[0]" placeholder="" required&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;input type="text" class="form-control" name="your_name[0]" placeholder="Retro Example - CRG321" required&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" class="form-control" name="your_name[0]" placeholder="50312341234" required&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" class="form-control" name="your_name[0]" placeholder="Brand Name Large Jam Tart" required&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" class="form-control" name="your_name[0]" placeholder="1 x 10 Pre Portioned" required&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" class="form-control" name="your_name[0]" placeholder="£6.00" required&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" class="form-control" name="your_name[0]" placeholder="" required&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" class="form-control" name="your_name[0]" placeholder="" required&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" class="form-control" name="your_name[0]" placeholder="£0.50" required&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;/div&gt;</pre></div>并将其显示在表格之外<table> </table> - Ho to access the data from inside html table tags <table> and display it outside of table

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何访问HTML标记内的JavaScript变量 - How to access the javascript variable inside html tags 访问 innerHTML 中的 html 标签 | javascript - get access to html tags inside innerHTML | javascript HTML标记内的Javascript - Javascript inside HTML tags Javascript函数中的HTML标记 - HTML tags inside Javascript function 在JavaScript中添加html标签 - Adding html tags inside JavaScript 何从 html 表标签内部访问数据<div id="text_translate"><p>我正在制作一个 HTML 表格并在表格内使用表格。 数据在表格内完美显示,但我想从表格中访问一些数据并将其显示在表格标签之外,我该怎么做? 非常感谢您的帮助</p><p>页面上的数据显示如下:</p><p> <a href="https://i.stack.imgur.com/CytCE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CytCE.png" alt="这就是桌子的样子"></a></p><p> 但我想要这样的东西:访问边表标签中的一列<a href="https://i.stack.imgur.com/VMST5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VMST5.png" alt="像这样"></a></p><pre> &lt;:DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;.-- CSS only --&gt; &lt;link rel="stylesheet" href="https.//stackpath.bootstrapcdn.com/bootstrap/4.5.1/css/bootstrap,min.css" integrity="sha384-VCmXjywReHh4PwowAiWNagnWcLhlEJLA5buUprzK8rxFgeH0kww/aWY76TfkUoSX" crossorigin="anonymous"&gt; &lt;,-- JS: Popper.js. and jQuery --&gt; &lt;script src="https.//code.jquery.com/jquery-3.5.1:slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"&gt;&lt;/script&gt; &lt;script src="https.//cdn.jsdelivr.net/npm/popper.js@1.16:1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"&gt;&lt;/script&gt; &lt;script src="https.//stackpath.bootstrapcdn.com/bootstrap/4.5.1/js/bootstrap:min;js" integrity="sha384-XEerZL0cuoUbHE4nZReLT7nx9gQrQreJekYhJD9WNWhH8nEW+0c5qq7aIo2Wl30J" crossorigin="anonymous"&gt;&lt;/script&gt; &lt;/head&gt; &lt;style&gt;:new { float;right: width;98%: margin-top;0: margin-right;20px: margin-bottom;0. margin-left:auto; }:descp{ background-color;lightgreen: font-size; 12px: font-weight: normal: } &lt;/style&gt; &lt;br&gt; &lt;div class= "head new"&gt; &lt;center&gt; &lt;h1&gt; PROMOTION PROPOSAL &lt;/h1&gt; &lt;/center&gt; &lt;/div&gt; &lt;div id="container" class="new"&gt; &lt;table table id="example" class="table table-bordered table-striped " style="width:100%" &gt; &lt;col style="width:14%"&gt; &lt;col style="width:9%"&gt; &lt;col style="width:15%"&gt; &lt;col style="width:8%"&gt; &lt;col style="width:6%"&gt; &lt;col style="width:6%"&gt; &lt;col style="width;6%"&gt; &lt;col style="width:6%"&gt; &lt;thead&gt; &lt;tr&gt; &lt;th colspan="5"&gt;&lt;/th&gt; &lt;th style="background-color;powderblue:text-align;center:"colspan="2"&gt;OFF INVOICE&lt;/th&gt; &lt;th style="background-color;powderblue.text-align.center."colspan="3"&gt;RETROSPECTIVE&lt;/th&gt; &lt;th colspan="2"&gt;&lt;/th&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th&gt;Product Code&lt;/th&gt; &lt;th&gt;Outer Barcode&lt;/th&gt; &lt;th&gt;Product Description&lt;/th&gt; &lt;th&gt;Pack Size &amp; Weight&lt;/th&gt; &lt;th&gt;Standard Invoice Cost of Product&lt;/th&gt; &lt;th&gt;Bonus&lt;/th&gt; &lt;th&gt;Net Cost&lt;/th&gt; &lt;th&gt;Retro Bonus&lt;/th&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th class="descp" colspan="1"&gt;Enterde in this column&lt;/th&gt; &lt;th class="descp" colspan="1"&gt;Enter your OUTER barcode in this column&lt;/th&gt; &lt;th class="descp" colspan="1"&gt;Enter the full product name with how you would like&lt;/th&gt; &lt;th class="descp" colspan="1"&gt;Pack size &amp; weight in this box or numb&lt;/th&gt; &lt;th class="descp"colspan="1"&gt;Standard invoice cost of product&lt;/th&gt; &lt;th class="descp" colspan="1"&gt;Enter here how&lt;/th&gt; &lt;th class="descp" colspan="1"&gt;Net&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;&lt;input type="text" class="form-control" name="your_name[0]" placeholder="Off Invoice example -CRG123" required&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" class="form-control" name="your_name[0]" placeholder="50212341234" required&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" class="form-control" name="your_name[0]" placeholder="Brand Name Crunchy Cornflakes" required&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" class="form-control" name="your_name[0]" placeholder="£25 x 250g" required&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" class="form-control" name="your_name[0]" placeholder="£3.05" required&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" class="form-control" name="your_name[0]" placeholder="£0.5" required&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" class="form-control" name="your_name[0]" placeholder="£2.55" required&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" class="form-control" name="your_name[0]" placeholder="" required&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;input type="text" class="form-control" name="your_name[0]" placeholder="Retro Example - CRG321" required&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" class="form-control" name="your_name[0]" placeholder="50312341234" required&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" class="form-control" name="your_name[0]" placeholder="Brand Name Large Jam Tart" required&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" class="form-control" name="your_name[0]" placeholder="1 x 10 Pre Portioned" required&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" class="form-control" name="your_name[0]" placeholder="£6.00" required&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" class="form-control" name="your_name[0]" placeholder="" required&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" class="form-control" name="your_name[0]" placeholder="" required&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" class="form-control" name="your_name[0]" placeholder="£0.50" required&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;/div&gt;</pre></div>并将其显示在表格之外<table> </table> - Ho to access the data from inside html table tags <table> and display it outside of table 使用 javaScript 在 html 标签内创建 html 标签? - Creating html tags inside html tags using javaScript? 通过香草JavaScript生成HTML标签 - Generating HTML tags through vanilla javascript 在脚本中使用HTML表格标签 - Using HTML table tags inside script 在html标签内执行Javascript函数 - Executing an Javascript function inside html tags
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM