javascript document.getElementById 返回 [object HTMLTableCellElement]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27026960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:51:06  来源:igfitidea点击:

document.getElementById returns [object HTMLTableCellElement]

javascripthtml

提问by lordterrin

Right now, I have this td:

现在,我有这个 td:

<td id="setCustomer"><?= $customer ?></td>

Later down the page I have:

后来在页面上我有:

<script>
document.getElementById('setCustomer').onclick = function(){
var newOne = document.getElementById('setCustomer');
console.log("var newOne is "+newOne); 
}
</script>

When I click the table cell that contains this customer name, the function launches and spits out:

当我单击包含此客户名称的表格单元格时,该函数将启动并吐出:

var newOne is [object HTMLTableCellElement]

instead of:

代替:

var newOne is ACME CORP.

what am I doing wrong?

我究竟做错了什么?

回答by Scimonster

You need the content of the element, not the element itself:

您需要元素的内容,而不是元素本身:

var newOne = document.getElementById('setCustomer').innerHTML;