简体   繁体   English

无法<span>从回调中</span>更新<span>(分配给变量的)</span> textContent

[英]Can't update textContent of <span> (that is assigned to a variable) from within the callback

I have a simple HTML page 我有一个简单的HTML页面

在此处输入图片说明

When I press on buttons "Player 1" and "Player 2", I want changes to reflect on the page. 当我按下“播放器1”和“播放器2”按钮时,我希望更改反映在页面上。 In console scores go up, everything's fine, but why page doesn't update the changed score? 在控制台得分上升时,一切都很好,但是为什么页面不更新更改的得分? ( let p1score and let p2score ) let p1scorelet p2score

Here's my code, thanks ! 这是我的代码,谢谢!

const p1button = document
    .querySelector('#p1')
    .addEventListener('click', scoreUp);
const p2button = document
    .querySelector('#p2')
    .addEventListener('click', scoreUp);

let p2score = document.querySelector('#p2score').textContent;
let p1score = document.querySelector('#p1score').textContent;

function scoreUp(e) {
    if (e.target.textContent === 'Player 1') {
        p1score++;
        console.log(p1score);
    } else {
        p2score++;
        console.log(p2score);
    }
}

When you do let p1score = document.querySelector('#p1score').textContent; 当您这样做时, let p1score = document.querySelector('#p1score').textContent; you are effectively saving the value of the text content, not a reference to that value. 您实际上是在保存文本内容的值,而不是对该值的引用 Thus, p1score and p2score hold their own unique copies of the values (integers) of the text content. 因此, p1scorep2score拥有它们自己的文本内容值(整数)的唯一副本。

So, when you increment p1score and p2score you are only incrementing the values in held by these variables. 因此,当您递增p1scorep2score您只是在递增这些变量所持有的值。 Thus, once you've incremented your value, you should set the text content to be the incremented value by doing 因此,增加值后,应通过执行以下操作将文本内容设置为增加的值

pScoreOneElem.textContent = p1score;

pScoreTwoElem.textContent = p2score;

This will effectively update the text content by changing its value to reflect the changes to p1score and p2score : 通过更改其值以反映对p1scorep2score的更改,这将有效地更新文本内容:

 const p1button = document .querySelector('#p1') .addEventListener('click', scoreUp); const p2button = document .querySelector('#p2') .addEventListener('click', scoreUp); let pScoreOneElem = document.querySelector('#p1score'); let pScoreTwoElem = document.querySelector('#p2score'); let p2score = pScoreTwoElem.textContent; let p1score = pScoreOneElem.textContent; function scoreUp(e) { if (e.target.textContent === 'Player 1') { p1score++; pScoreOneElem.textContent = p1score; console.log(p1score); } else { p2score++; pScoreTwoElem.textContent = p2score; console.log(p2score); } } 
 <button id="p1">Player 1</button> <button id="p2">Player 2</button> <br /> <div class="player-scores"> Player 1 Score: <span id="p1score">0</span> <br /> Player 2 Score: <span id="p2score">0</span> </div> 

let p2score = document.querySelector('#p2score').textContent only assigns the value of textContent to the score variable. let p2score = document.querySelector('#p2score').textContent仅将let p2score = document.querySelector('#p2score').textContent的值分配给score变量。 That value is no longer connected to the element text content property. 该值不再连接到元素文本内容属性。 You must reference the element instead 您必须改为引用该元素

const p1button = document
    .querySelector('#p1')
    .addEventListener('click', scoreUp);
const p2button = document
    .querySelector('#p2')
    .addEventListener('click', scoreUp);

let p1 = document.querySelector('#p1score');
let p2 = document.querySelector('#p2score');
let p1score = p1.textContent;
let p2score = p2.textContent;

function scoreUp(e) {
    if (e.target.textContent === 'Player 1') {
        p1.textContent = p1score++;
        console.log(p1.textContent);
    } else {
        p2.textContent = p2score++;
        console.log(p2.textContent);
    }
}

You need to set scores to p1score and p2score elements like so : 您需要将分数设置为p1scorep2score元素,如下所示:

if (e.target.textContent === 'Player 1') {
    p1score++;
    document.querySelector('#p1score').textContent = p1score;
    console.log(p1score);
} else {
    p2score++;
    document.querySelector('#p2score').textContent = p2score;
    console.log(p2score);
}

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

相关问题 无法写入 javascript 回调中的 textContent 属性 - Can't write to textContent property inside javascript callback 如何获取分配给带有 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? 为什么我不能在 useEffect 回调中更新我的组件的 state? - Why can't I update the state of my component from within a useEffect callback? 如何从异步回调函数中更新全局变量? - How to update global variable from within a async callback function? 无法从量角器回调中访问数组 - Can't access array from within protractor callback 无法从angular.forEach循环中更新全局变量 - Can't update a global variable from within an angular.forEach loop 无法在Ionic2的Callback中访问本地属性 - Can't access local property from within Callback in Ionic2 如果声明并分配了 object,我如何更改 DOM 中的 textContent - How can I change textContent in DOM if object is declared and assigned 从变量分配时,分配的css类不起作用 - assigned css class doesn't work when assigned from variable 为什么我不能在 if 语句中更改变量的 textContent 属性? - Why can't I change a variable's textContent's atribute in an if-statement?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM