为啥 Uncaught TypeError: Cannot set property 'innerHTML' of null 显示即使 <span> 和 js 中的 id 相同? [复制]

Posted

技术标签:

【中文标题】为啥 Uncaught TypeError: Cannot set property \'innerHTML\' of null 显示即使 <span> 和 js 中的 id 相同? [复制]【英文标题】:Why is the Uncaught TypeError: Cannot set property 'innerHTML' of null showing even though the id are the same in <span> and js? [duplicate]为什么 Uncaught TypeError: Cannot set property 'innerHTML' of null 显示即使 <span> 和 js 中的 id 相同? [复制] 【发布时间】:2021-01-04 11:45:03 【问题描述】:

html

<span id=“customer-name”> customer </span>

Js:

var customerName = "Harry";
document.getElementById("customer-name").innerHTML = customerName;

【问题讨论】:

“customer-name” 有花引号 用“简单”双引号替换“智能引号”。如果这没有帮助,请阅读this。 您还需要粘贴 HTML,以便我们看到完整的图片。 javascript 与 HTML 的关系在哪里?是在它之前吗? @mplungjan 怎么样?这些花引号被解析为 HTML 中 ID 的一部分,请参阅 fiddle。 @Teemu 你在作弊document.getElementById('“things”'); 【参考方案1】:

如果您直接复制粘贴您的代码,那是因为您在 HTML 中拥有时髦的“智能引号”。这可能是您的编辑器自动执行的操作,并且您希望在编写代码时将其关闭。

更正:

<span id="customer-name"> CUSTOMER </span>

【讨论】:

请不要回答typo-type问题,因为该问题无法删除。在这种情况下,错字仅在问题中,而不在实际代码中 非常感谢!它奏效了。我看不到任何错误,但它显示错误。 @mplungjan 我看不出你怎么知道这不是问题所在。使用错误的引号确实会导致错误,并且 OP 刚刚确认它修复了错误...... 当我投票以欺骗或错别字结束时,我已经作为评论回答了这个问题 @LeeWillis 这个问题不值得回答。有骗子,这是一个错字类型的问题。我不同意自己在关闭时选择哪个,并且 OP 无法删除已回答的问题。与热衷于投反对票无关

以上是关于为啥 Uncaught TypeError: Cannot set property 'innerHTML' of null 显示即使 <span> 和 js 中的 id 相同? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

为啥 Uncaught TypeError: Cannot set property 'innerHTML' of null 显示即使 <span> 和 js 中的 id 相同? [复制]

Uncaught (in promise) TypeError: Cannot set properties of null (setting 'innerText') in OpenWetherMa

Uncaught (in promise) TypeError: Cannot read properties of null (reading 'fingerprint') Laravel Live

如何解决问题:Uncaught (in promise) TypeError: promisify is not a function?

Uncaught TypeError

Uncaught TypeError: Cannot redefine property: $router