当我单击此处并存储在数组中时,试图获取按钮的值?知道我在做什么错吗?

问题描述 投票:0回答:1

我正在尝试能够单击并在div中选择四个按钮之一。div是五分之一-因此,当我单击时,我只想检查确切的按钮,并且属性更改会影响正确的div按钮嵌套在div内,如下所示。我可以使用事件处理程序来确认单击哪个div,然后使用if语句从那里进行控制吗?

我遇到的最后一个问题是,我想将innerHTML推送到一个数组中,然后进行检查,但我一直收到未定义的错误

HTML ..........

 <div class="rightMain" id="q1box">
   <div class="answerButtonBox">
   <button type="button" class="answerButton" id="q1A" value="1">The Marauder</button>
   </div>
   <div class="answerButtonBox">
   <button type="button" class="answerButton" id="q1B" value="2">The Black Pearl</button>
   </div>
 </div>

Typescript / JS ....

    var userGuess:[{}];       ///can i just initilize an array and then push the innerhtml into it?
    var answers: [{}];


    question1.addEventListener("click", checkq1)

    function checkq1(){
    if (first.onclick)
    {
        first.setAttribute("style", "background-color: yellow;")
        second.setAttribute("style", "background-color: grey;")
        third.setAttribute("style", "background-color: grey;")
        fourth.setAttribute("style", "background-color: grey;")
        userGuess.push(first.innerHTML)
    }
    if (second.onclick)
    {
        first.setAttribute("style", "background-color: grey;")
        second.setAttribute("style", "background-color: yellow;")
        third.setAttribute("style", "background-color: grey;")
        fourth.setAttribute("style", "background-color: grey;")
        userGuess.push(second.innerHTML)
    }

etc..... 

任何帮助将不胜感激!

关于我的数组的错误即时消息。...

index.ts:59未捕获的TypeError:无法读取未定义的属性'toString' 在HTMLDivElement.checkq1(index.ts:59)

javascript typescript dom-manipulation
1个回答
0
投票

您可以获取所有未选择的答案并获取CSS,希望能对您有所帮助

© www.soinside.com 2019 - 2024. All rights reserved.