显示新段落的问题



我正在创建一个游戏,比如石头、纸、剪刀,一切都很好,但我也想显示用户选择了什么,但我没有得到,这是代码

var paragraph = document.querySelector('p');
var assignClick = function enter code here(tag, pos) {
tag.addEventListener('click', function() {
playerChoice = pos;
cpuChoice.init();
paragraph.innerText = 'You chose:' + playerChoice;
paragraph.innerText = 'The computer chose:' + cpuChoice.text;
paragraph.innerText += 'n' + chooseWinner(playerChoice, cpuChoice.store);
paragraph.innerText += 'n' + 'SCORE: ' + score;
});
}

我是JavaScript的新手,但根据我的猜测,类似paragraph.innerText='You choosed:'+playerChoice;应该做的工作

要创建新的段落元素,您需要使用document.createElement("p"((有关更多信息,请查看:document.createElement.

创建元素后,您需要使用parent.appendChild((方法将其手动插入到dom中(有关更多信息,请查看:parent.append Child

编辑

我认为以下代码将为您完成工作:

paragraph.innerText = 'The computer chose: ' + cpuChoice.text;
paragraph.innerText += 'n'+'The player chose: ' +readable[playerChoice];

最新更新