Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我应该如何在之前(在不同的函数中)引用一个元素,并在新函数中修改它?_Javascript_Html_Css - Fatal编程技术网

Javascript 我应该如何在之前(在不同的函数中)引用一个元素,并在新函数中修改它?

Javascript 我应该如何在之前(在不同的函数中)引用一个元素,并在新函数中修改它?,javascript,html,css,Javascript,Html,Css,如何引用我创建的特定破折号,并添加 给他们的信。阅读注释和代码以获得更好的效果 上下文谢谢你事先的帮助 <!Doctype html> <html lang="en"> <head> <style> ul { display: inline; list-style-type: none; } .boxes { font-size:1.6em; text-align:center; width: 10px; border-bottom: 3px s

如何引用我创建的特定破折号,并添加 给他们的信。阅读注释和代码以获得更好的效果 上下文谢谢你事先的帮助

<!Doctype html>
<html lang="en">
<head>
<style>
ul {
display: inline;
list-style-type: none;
}

.boxes {
font-size:1.6em;
text-align:center;
width: 10px;
border-bottom: 3px solid black;
margin: 5px;
padding: 10px;
display: inline;
}
.hidden {
visibility: hidden;
}
.visible {
visibility: visible;
}

</style>
</head>
<body>
<script>

var possibleWord = ["COW", "BETTER", "HARDER", "JUSTIFY", "CONDEMN", 
"CONTROL", "HELLO", "UNDERSTAND", "LIFE", "INSIGHT","DATE", 
"RIGHTEOUSNESS"];
var hangmanWord = possibleWord[Math.floor(Math.random() * 
possibleWord.length)];
var underlineHelp;
var space;
var guess;
var guesses = [];
var placement;
var underscores = [];
var character = [];
var textNodes = [];
window.onload = function () {
placement = document.getElementById('hold');
underlineHelp = document.createElement('ul');
placement.appendChild(underlineHelp);
for (i = 0; i < hangmanWord.length; i++) {
underscores = document.createElement('li');
underscores.setAttribute('class', 'boxes');
guesses.push(underscores);
underlineHelp.appendChild(underscores);
character = document.createElement('span');
character.appendChild(document.createTextNode(hangmanWord[i]));
character.classList.add('hidden');
underscores.appendChild(character);
}

保险商实验室{
显示:内联;
列表样式类型:无;
}
.盒子{
字号:1.6em;
文本对齐:居中;
宽度:10px;
底部边框:3倍纯黑;
保证金:5px;
填充:10px;
显示:内联;
}
.隐藏{
可见性:隐藏;
}
.可见{
能见度:可见;
}
var possibleWord=[“奶牛”、“更好”、“更难”、“证明”、“谴责”,
“控制”,“你好”,“理解”,“生活”,“洞察力”,“日期”,
“正义”];
var hangmanWord=possibleWord[Math.floor(Math.random()*
可能长度];
var-underlineHelp;
var空间;
var猜测;
var猜测=[];
var配置;
var下划线=[];
变量字符=[];
var textNodes=[];
window.onload=函数(){
placement=document.getElementById('hold');
underlineHelp=document.createElement('ul');
安置。附属物儿童(下属帮助);
对于(i=0;i
这就是我稍后要提到的领域

for(x=1;x<=26;x++){
document.getElementById("test").innerHTML = hangmanWord;
var btn = document.createElement("BUTTON");
var myP = document.createElement("br");


var letter = String.fromCharCode(x+64);
var t = document.createTextNode(letter);

btn.appendChild(t);

btn.id = letter;

对于(x=1;x您可以将猜到的单词和猜到的字符存储在对象中,只输出替换后的结果。对我来说似乎更容易

<html>
    <head>
        <script>
            //The hangman object
            var Hangman = {
                wordToGuess: 'RIGHTEOUSNESS', //The word to guess. Just one for my example
                guessedLetters: [] //The guessed characters
            };

            window.onload = function(){
                //Creating the buttons
                for(var tF=document.createDocumentFragment(), x=1; x<=26; x++){
                    var tLetter = String.fromCharCode(x+64),
                        tButton = tF.appendChild(document.createElement("button"));

                    tButton.id = tLetter;
                    tButton.addEventListener("click", checkLetter);
                    tButton.appendChild(document.createTextNode(tLetter));

                    (x%10 === 0) && tF.appendChild(document.createElement('br'))
                };

                document.body.appendChild(tF);
                startTheGame()
            };

            //Starts a game of hangman
            function startTheGame(){
                var tWord = Hangman.wordToGuess,
                    tPlacement = document.getElementById('hold');

                document.getElementById('test').innerHTML = tWord;

                //Resetting the guesses
                Hangman.guessedLetters = [];

                //Creating dashes for all letters in our word
                for(var i=0, j=tWord.length; i<j; i++){
                    tPlacement.appendChild(document.createTextNode('_'))
                }
            }

            function checkLetter(){
                var tButton = this,
                    tLetter = tButton.id,
                    tWord = Hangman.wordToGuess,
                    tPlacement = document.getElementById('hold');

                //Make a guess
                document.getElementById('p1').innerHTML += tLetter;
                (Hangman.guessedLetters.indexOf(tLetter) === -1) && Hangman.guessedLetters.push(tLetter.toLowerCase());

                //Clear the current word
                while(tPlacement.firstChild) tPlacement.removeChild(tPlacement.firstChild);

                //Now we reverse replace the hangman word by the guessed characters
                for(var i=0, j=tWord.length; i<j; i++){
                    tPlacement.appendChild(document.createTextNode(
                        (Hangman.guessedLetters.indexOf(tWord[i].toLowerCase()) === -1) ? '_' : tWord[i]
                    ))                  
                }
            }
        </script>
    </head>

    <body>
        <p>Click the button to make a BUTTON element with text.</p>
        <div id = 'contents'>
            <div id = 'hold'></div>
        </div>
        <p id = 'p1'> Letters picked: </p>
        <div id= "picBox"></div>
        <div id = "test"></div>

        <!-- This one seems to be too much
            </div>
        -->
    </body>
</html>

//刽子手目标
var Hangman={
wordToGuess:'正义'//要猜的词。我举一个例子
猜测的字母:[]//猜测的字符
};
window.onload=函数(){
//创建按钮

for(var tF=document.createDocumentFragment(),x=1;xd不要因为你能写就用大写字母写。大喊大叫是粗鲁的。对不起,我不是说大喊大叫,只是为了让人们注意到我在哪里struggling@Anonymous更能帮助您的是格式化您的代码,而不是大声喊叫,并清楚地说明您的问题。对不起,我对堆栈溢出不太熟悉。我不会尝试太粗鲁了。谢谢你的建议!当我有另一个问题时,我会改进的。为什么不改进这个问题呢?好的,我已经试过了。如果我把所有其他单词都放进去(并生成随机单词),这是不起作用的。它在JSFIDLE上有效,但在浏览器上不起作用(比如谷歌chrome)。请您使用浏览器界面检查代码。如果可以,请尝试在对象初始值设定项中设置一个数组,然后让它输出一个随机单词。我非常感谢您抽出时间来帮助我。干杯!哦,别担心,我遇到了一个尴尬的语法错误!谢谢您的帮助。您的代码让我意识到另一个w很高兴看到这个目标。非常感谢!有点喜欢它,所以我在更新中做了一个适当的拆分版本。如果它满足您的要求,请不要忘记接受答案。
</script>

</head>
<body>

<p>Click the button to make a BUTTON element with text.</p>
<div id = "contents">
<div id = "hold"></div>
</div>
<p id ="p1"> Letters picked: </p>
<div id= "picBox"></div>
<div id = "test"></div>



</div>
</body>
<html>
    <head>
        <script>
            //The hangman object
            var Hangman = {
                wordToGuess: 'RIGHTEOUSNESS', //The word to guess. Just one for my example
                guessedLetters: [] //The guessed characters
            };

            window.onload = function(){
                //Creating the buttons
                for(var tF=document.createDocumentFragment(), x=1; x<=26; x++){
                    var tLetter = String.fromCharCode(x+64),
                        tButton = tF.appendChild(document.createElement("button"));

                    tButton.id = tLetter;
                    tButton.addEventListener("click", checkLetter);
                    tButton.appendChild(document.createTextNode(tLetter));

                    (x%10 === 0) && tF.appendChild(document.createElement('br'))
                };

                document.body.appendChild(tF);
                startTheGame()
            };

            //Starts a game of hangman
            function startTheGame(){
                var tWord = Hangman.wordToGuess,
                    tPlacement = document.getElementById('hold');

                document.getElementById('test').innerHTML = tWord;

                //Resetting the guesses
                Hangman.guessedLetters = [];

                //Creating dashes for all letters in our word
                for(var i=0, j=tWord.length; i<j; i++){
                    tPlacement.appendChild(document.createTextNode('_'))
                }
            }

            function checkLetter(){
                var tButton = this,
                    tLetter = tButton.id,
                    tWord = Hangman.wordToGuess,
                    tPlacement = document.getElementById('hold');

                //Make a guess
                document.getElementById('p1').innerHTML += tLetter;
                (Hangman.guessedLetters.indexOf(tLetter) === -1) && Hangman.guessedLetters.push(tLetter.toLowerCase());

                //Clear the current word
                while(tPlacement.firstChild) tPlacement.removeChild(tPlacement.firstChild);

                //Now we reverse replace the hangman word by the guessed characters
                for(var i=0, j=tWord.length; i<j; i++){
                    tPlacement.appendChild(document.createTextNode(
                        (Hangman.guessedLetters.indexOf(tWord[i].toLowerCase()) === -1) ? '_' : tWord[i]
                    ))                  
                }
            }
        </script>
    </head>

    <body>
        <p>Click the button to make a BUTTON element with text.</p>
        <div id = 'contents'>
            <div id = 'hold'></div>
        </div>
        <p id = 'p1'> Letters picked: </p>
        <div id= "picBox"></div>
        <div id = "test"></div>

        <!-- This one seems to be too much
            </div>
        -->
    </body>
</html>
<html>
    <head>
        <style>
            button{
                margin: 1px;
                min-width: 30px
            }

            button[disabled]{
                background: #777;
                color: #fff
            }
        </style>

        <script>
            //Handles the hangman game itself
            ;(function(ns){
                'use strict';

                var _listOfWord = ['COW', 'BETTER', 'HARDER', 'JUSTIFY', 'CONDEMN', 'CONTROL', 'HELLO', 'UNDERSTAND', 'LIFE', 'INSIGHT','DATE', 'RIGHTEOUSNESS'],
                    _wordToGuess = null, //The word to guess. Just one for my example
                    _guessedLetters = [] //The guessed characters

                ns.Hangman = {
                    //Returns the current obstructed word
                    getCurrentObstructedWord: function(){
                        var tWord = []; //The current state of the game

                        if(_wordToGuess){
                            //Now we reverse replace the hangman word by the guessed characters
                            for(var i=0, j=_wordToGuess.length; i<j; i++){
                                tWord.push(
                                    (_guessedLetters.indexOf(_wordToGuess[i].toLowerCase()) === -1) ? '_' : _wordToGuess[i]
                                )
                            }
                        };

                        return tWord
                    },

                    //Make a guess at the current game
                    Guess: function(letter){
                        //Add the guess to the list of guesses, unless already guessed
                        (_guessedLetters.indexOf(letter) === -1) && _guessedLetters.push(letter.toLowerCase());

                        return this.getCurrentObstructedWord()
                    },

                    isComplete: function(){
                        return !!(this.getCurrentObstructedWord().indexOf('_') === -1)
                    },

                    //Starts a new game
                    Start: function(){
                        _guessedLetters = []; //Resetting the guesses
                        _wordToGuess = _listOfWord[Math.floor(Math.random() * _listOfWord.length)];

                        return _wordToGuess
                    }
                }
            }(window._ = window._ || {}));

            //Creates the buttons for hangman
            function createButtons(){
                var tContainer = document.querySelector('#buttons');
                while(tContainer.firstChild) tContainer.removeChild(tContainer.firstChild);

                //Creating the buttons
                for(var tF=document.createDocumentFragment(), x=1; x<=26; x++){
                    var tLetter = String.fromCharCode(x+64),
                        tButton = tF.appendChild(document.createElement('button'));

                    tButton.id = tLetter;
                    tButton.addEventListener('click', makeAGuess);
                    tButton.appendChild(document.createTextNode(tLetter));

                    (x%10 === 0) && tF.appendChild(document.createElement('br'))
                };

                tContainer.appendChild(tF)
            };

            //Makes a guess
            function makeAGuess(){
                var tButton = this,
                    tLetter = tButton.id;

                //Disable the button
                tButton.setAttribute('disabled', 'disabled');

                //Make a guess
                var tElement = document.getElementById('hold');
                tElement.textContent = _.Hangman.Guess(tLetter).join('');

                //Check if finished
                if(_.Hangman.isComplete()){
                    tElement.style.color = 'limegreen'
                }
            };

            //Starts a new game of hangman
            function Reset(){
                createButtons(); //Recreated the buttons
                _.Hangman.Start(); //Starting a game of hangman

                var tElement = document.getElementById('hold');
                tElement.textContent = _.Hangman.getCurrentObstructedWord().join('');
                tElement.style.color = ''
            };

            window.onload = function(){
                Reset()
            };
        </script>
    </head>

    <body>
        <div id = 'hold'></div>
        <p id = 'p1'>Make a guess:</p>
        <div id = 'buttons'></div>
        <br /><br />
        <button onclick = 'Reset()'>Start a new game</button>
    </body>
</html>