【问题标题】:Click event is not working in html DOM innerTEXT单击事件在 html DOM innerTEXT 中不起作用
【发布时间】:2022-01-01 08:09:09
【问题描述】:

我想做一个真心话大冒险网站,人们可以在线玩真心话大冒险,但我遇到了一些问题。

HTML 代码

<button id="truth"> +t </button>
<div><p id="game">+t for English Truth, +d For English Dare, +tb For Bangla Truth, +db for Bangla Dare</p></div>

这是我的js代码

const t = [
    "If you could be invisible, what is the first thing you would do?", 
    "What is a secret you kept from your parents?", 
    "What is the most embarrassing music you listen to?", 
    "What is one thing you wish you could change about yourself?",
    "Who is your secret crush?",
];
document.getElementById('truth').addEventListener('click', function(){
    const truth = t[Math.floor(Math.random() * t.length)];
 const games = document.getElementById('game');
 console.log(truth)
    games.innerText(truth)
})

但是当我点击按钮时,它显示 Uncaught TypeError: games.innerText is not a function

现在,我该怎么办?

【问题讨论】:

    标签: javascript html web


    【解决方案1】:

    它说 games.innerText 不是一个带参数的函数。 innerText 方法的正确使用方法是:

    
    games.innerText = truth;
    
    

    同innerHtml

    看看innerText docs from MDN

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-13
      • 2017-10-13
      • 1970-01-01
      • 2013-12-07
      • 2012-09-30
      • 2023-03-28
      • 1970-01-01
      相关资源
      最近更新 更多