gpt4 book ai didi

javascript - 从 HTML 表格中读取列中的文本

转载 作者:行者123 更新时间:2023-12-03 02:39:14 25 4
gpt4 key购买 nike

我已经调试了一段时间,试图获取表中列的值。我认为一旦完成此操作,就应该很容易将下一列中的值传递出我的 JS 函数。

我的 HTML 表格是:

<table id="country_LE_table" style = "display:none">
<tr>
<td>Japan</td>
<td>83.7</td>
</tr>
<tr>
<td>Switzerland</td>
<td>83.4</td>
</tr>
</table>

我的 JavaScript 是:

<script type="text/javascript">
function getLifeExpectancy() {
var Country = "<?php echo $Country ?>";
document.write(Country); // this gets read OK
var table = document.getElementById("country_LE_table");
var tr = table.getElementsByTagName("tr");
document.write(tr.length); document.write("<br>"); // works as expected

for (var i = 0; i < tr.length; i++) {

document.write(tr[i].innerHTML); document.write("<br>"); // works well up to here
// the following doesn't work
var td = tr[i].getElementsByTagName("td")[0];
if (td = Country) { //need td.fullHTML/value/fullText?
return tr[i].getElementsByTagName("td")[1]; // return the number
}
document.getElementById("demo").innerHTML = getLifeExpectancy();
</script>

如果我执行document.write(td),我的页面上会显示“[object HTMLTableCellElement]”
如果我执行document.write(td.fullHTML),我的页面上会出现“undefined”

当我探索 td.innerHTML 以外的其他方法时,我得到了这一点 - 看起来我无法使用基于文本的函数。

It looks like I can't use text-functions to get the contents of the row/column element.

最佳答案

用这个代替。您使用了赋值运算符而不是比较运算符“==”

if (td.innerHTML == Country)
{
}

关于javascript - 从 HTML 表格中读取列中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48399692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com