简体   繁体   English

似乎无法使用.innerHTML将文本添加到H1标记

[英]Can't seem to add text to an H1 tag using .innerHTML

I've tried everything on this website, on every website, but for some odd reason, I can't seem to add text to an h1 tag. 我在这个网站上尝试了所有内容,但是由于一些奇怪的原因,我似乎无法在h1标签上添加文字。 I've done this a million times before no issues but now it doesn't seem to work no matter what I do. 在没有问题之前我已经完成了这一百万次,但现在无论我做什么它似乎都没有用。 The onClick functions work perfectly fine tho, so I know the function is being ran. onClick函数完美地工作,所以我知道函数正在运行。

    const true1 = document.getElementsByClassName('True')
    const false1 = document.getElementsByClassName('False')
    const next = document.getElementsByClassName('next')
    const questione = document.getElementById('QuestionE')


function questionOne(){
      questione.innerHTML = "hello";

        true1[0].addEventListener('click', ((() => function aClickEvent(){
            true1[0].classList.toggle('TrueH')
             next[0].classList.toggle('nextS')
             }))(), false);

             false1[0].addEventListener('click', ((() => function aClickEvent(){
             false1[0].classList.toggle('FalseH')
             next[0].classList.toggle('nextS')
            }))(), false);

    }

   window.addEventListener('load', questionOne(), false);
window.addEventListener('load', questionOne(), false);

this is calling questionOne which returns nothing so you function is executing imediately before the DOM is there and you are attaching undefined as the function to call on load. 这是调用questionOne,它返回任何内容,所以你的函数在DOM存在之前立即执行,并且你将undefined附加为函数来调用load。

Try 尝试

window.addEventListener('load', questionOne, false);

Without the calling parenthesis. 没有调用括号。

Looks like you're trying to write immediately invoked function expressions, ie 看起来你正在尝试立即编写调用的函数表达式,即

(function blah(){})()

is this what you mean to do? 这是你的意思吗? You have click listeners, so maybe you meant something like this: 你有点击听众,所以也许你的意思是这样的:

function questionOne() {
  questione.innerHTML = "hello";

  true1[0].addEventListener('click', () => {
    true1[0].classList.toggle('TrueH')
    next[0].classList.toggle('nextS')
  }, false);

  false1[0].addEventListener('click', () => {
    false1[0].classList.toggle('FalseH')
    next[0].classList.toggle('nextS')
  }, false);

}

btw 'hello' appears in my test... btw'hello'出现在我的测试中......

This snippet works so I am assuming that you have incorrectly spelled QuestionE in the id as I cannot see your markup or the 这个片段有效,所以我假设你在id中拼错了QuestionE,因为我看不到你的标记或者

const questione = document.getElementById('QuestionE')

runs before The element exists in the DOM. 在元素存在于DOM之前运行。 Try putting the line in the function or console.log(questione) and see if it has a value. 尝试将该行放在函数或console.log(questione)中,看看它是否有值。

 const questione = document.getElementById('QuestionE') function questionOne(){ questione.innerHTML = "hello"; } window.addEventListener('load', questionOne, false); 
 <div id="QuestionE"></div> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM