Javascript 从HTML表读取列中的文本

Javascript 从HTML表读取列中的文本,javascript,html,html-table,Javascript,Html,Html Table,我已经调试了一段时间,试图获取表中某列的值。我认为,一旦我完成了这项工作,就可以很容易地将下一列中的值从JS函数中传递出去 我的HTML表格是: <table id="country_LE_table" style = "display:none"> <tr> <td>Japan</td> <td>83.7</td> </tr> <tr> &

我已经调试了一段时间,试图获取表中某列的值。我认为,一旦我完成了这项工作,就可以很容易地将下一列中的值从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.writetd,我会在页面上获得[object HTMLTableCellElement]。 如果我使用document.writetd.fullHTML,我的页面上就会出现未定义的内容

当我探索td.innerHTML以外的其他方法时,我得到了这样一个结果——看起来我不能使用基于文本的函数


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

if (td.innerHTML == Country)
    {
    }

代码中的td是对cell元素本身的引用,您必须读取该元素的textContent或innerHTML属性。另外,document.write用于创建新文档,而不是操作现有文档。请停止猜测,如果您猜测属性名称,您将永远无法完成任何操作。阅读、学习并开始编写代码……这与document.getelementbyiddemodd.innerHTML=td.textContent一起起作用;