简体   繁体   English

我在显示带有 javascript 的 html 按钮时遇到问题

[英]I'm having trouble displaying an html button with javascript

I have two buttons on an HTML page, and I'm trying to show them once a button is pressed, but only one actually displays.我在 HTML 页面上有两个按钮,我试图在按下按钮后显示它们,但实际上只有一个显示。

This is the HTML这是 HTML

<button onclick="testLeft()" class="item-description" id= "left-item-description">.</button>
<button onclick="testRight()" class="item-description" id= "right-image-description">.</button>

This is the CSS这是 CSS

.item-description {
    display: none;
    box-sizing: border-box;
    position: fixed;
    z-index: 4;
    font-size: 35px;
    font-weight: 600;
    height: auto;
    top: 30%;
    padding: 10px 10px;
    color:white;
    background-color: transparent;
    border-radius: 4px;
    border: 3px solid black;
    transition: all 0.2s ease;
}


#left-item-description {
    margin-left: 10%;
    margin-right: 60%;
}

#right-image-description {
    margin-right: 10%;
    margin-left: 60%;
}

And this is the javascript这是 javascript

function newGame(){
    score = -1;
    increaseScore();
    correct = true;

    for (let i = 0; i < used.length; i++){
        used[i] = false;
    }

    indexLeft = shuffleIndex();
    indexRight = shuffleIndex();
    var left = document.getElementById("left-item-description");
    var righ = document.getElementById("right-item-description");
    
    left.style.display = "block";
    left.innerText = items[indexLeft];
    document.getElementById("left-item-image").src=images[indexLeft];
    
    righ.style.display = "block";
    righ.innerText = items[indexRight];
    document.getElementById("right-item-image").src=images[indexRight];

}

The left button works perfectly how I want it to, but for some reason, the right button doesn't display左键按我想要的方式完美运行,但由于某种原因,右键不显示

The element ID for your right button is "right-image-description" but in your Javascript you are trying to get "right-item-description".您的右键的元素 ID 是“right-image-description”,但在您的 Javascript 中,您正试图获得“right-item-description”。

change the id attribute from this: var righ = document.getElementById("right-item-description");从此更改 id 属性: var righ = document.getElementById("right-item-description"); to var righ = document.getElementById("right-image-description");var righ = document.getElementById("right-image-description"); and it should work.它应该可以工作。

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

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