简体   繁体   English

如果声明并分配了 object,我如何更改 DOM 中的 textContent

[英]How can I change textContent in DOM if object is declared and assigned

right now this line of code shows whats in DOM现在这行代码显示了 DOM 中的内容

let scoreDisplay = document.querySelector('.score').textContent

but if i want to change the textContent it doesnt work, why?但是如果我想更改 textContent 它不起作用,为什么?

scoreDisplay = "twenty five"

the change is visible only in console.log but not in DOM, how can i make it affect the DOM as well?更改仅在 console.log 中可见,但在 DOM 中不可见,我怎样才能使其也影响 DOM?

You are changing the value of the variable, not of the element itself.您正在更改变量的值,而不是元素本身的值。 Try modifying your code to尝试将您的代码修改为

let scoreDisplay = document.querySelector('.score')
scoreDisplay.textContent = "twenty five"

or even as a one-liner:甚至作为单行:

document.querySelector('.score').textContent= "twenty five"

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

相关问题 如何获取分配给带有 textContent 的变量的内容,以作为 javascript 中我的播放器输入的参数? - how can i get what is being assigned to a variable with textContent to act as an argument for my player input in javascript? 如何通过javascript更改textContent? - How to change textContent via javascript? 如何从 Javascript 中的 DOM 中的标签返回文本内容 - How to return textcontent from tags in DOM in Javascript 如何获取出现在父标签之前的 textContent? - How can I get the textContent that appears before the parent tag? 无法<span>从回调中</span>更新<span>(分配给变量的)</span> textContent - Can't update textContent of <span> (that is assigned to a variable) from within the callback 如何在 SAPUI5 中为页面中的每个 DOM 对象更改 css - How can I change css for every DOM object in the page in SAPUI5 如何在分配给数组索引的对象上调用方法? - How can I call a method on an object assigned to an array index? Javascript Dom元素ID分配给JS对象? 我疯了还是......? - Javascript Dom element ID assigned to JS object? Am I insane or…? DOM操作后TextContent的长度不变 - TextContent length doesn't change after DOM manipulation 如何使用 DOM (*object*.style) 更改 iframe 的灰度? - How do I change the grayscale of an iframe using DOM (*object*.style)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM