将Hr元素替换为适用于Hangman游戏的正确字母-javascript

时间:2018-12-15 06:29:33

标签: javascript

根据单词的长度生成破折号后,我将如何用用户输入的正确字母替换这些破折号。在大多数示例中,我已经看到人们为此目的使用数组。但是,由于我的破折号是hr元素,我应该怎么做?

以下是我的JavaScript代码:

var words = ['quaffle', 'bludger', 'pensieve', 'harry', 'lupin',
    'butterbeer', 'polyjuice', 'patronus', 'horcrux', 'voldemort'
];
correctletters = '';
missedletters = '';

function getRandomWord() {
    randomWord = words[Math.floor(Math.random() * words.length)];
    wordlength = randomWord.length;
}

function drawDashes() {
    for (var i = 0; i < wordlength; i++) {
        var dash = document.createElement("HR")
        document.body.appendChild(dash);
    }
}

function getGuess() {
    letterGuessed = document.getElementById("inputfield").value.toLowerCase();
    document.getElementById("inputfield").value = null;
    alert(randomWord);
    alert(letterGuessed);
    r = randomWord.includes(letterGuessed);

    if (r == true) {
        correctletters = correctletters + letterGuessed;
        alert("correct letters: " + correctletters);
    } else {
        missedletters = missedletters + letterGuessed;
        alert("missed letters: " + missedletters);
    }

    guess = missedletters + correctletters;
    displayGuessedLetters();
}


function displayGuessedLetters() {
    guessedletters = document.createElement("div");
    content = document.createTextNode(guess);
    guessedletters.appendChild(content);
    container = document.getElementById("inputcontainer");
    container.appendChild(guessedletters);
    guessedletters.style.cssText = 'font-family: "Josefin Sans"; font-size: 
    30 px;
    text - transform: uppercase;
    color: rgb(255, 85, 49);
    ';
}

function replaceDashes() {
    /*Code to replace dashes here.*/
}

1 个答案:

答案 0 :(得分:0)

您可以按照以下步骤进行操作:

function replaceDashes()
{
   var list = document.getElementsByTagName("hr")[0];
   list.parentNode.removeChild(list);
}