简体   繁体   English

如何使用javascript重复HTML元素n次

[英]How to repeat an html element n number of times using javascript

I'm trying to print an element, in my case an hr tag some number of times according to the length of a word. 我正在尝试打印一个元素,在我的情况下,是根据单词的长度多次标记hr标签。 This code is for a hangman game I'm trying to recreate. 该代码用于我要重新创建的子手游戏。 I have looked up similar questions and its not quite what I'm lookin for. 我已经查找了类似的问题,但它并不是我想要的。

This is my javascript code so far. 到目前为止,这是我的JavaScript代码。

var words = ['Quaffle', 'Bludger', 'Golden Snitch', 'Time-Turner', 
'Pensieve', 'Mirror of Erised'];

function getRandomWord(){
   var randomIndex = words[Math.floor(Math.random()* words.length)]; 
   alert(randomIndex);
}

function printDashes(){
   var dashes = document.getElementById("dash")
}

getRandomWord()
printDashes()

I'm not sure what to add after retrieving the element. 我不确定在检索元素后要添加什么。 Can someone guide me on how to go about this? 有人可以指导我如何解决吗?

You can also create div's so you can enter letters when the user inputs a character. 您还可以创建div,以便在用户输入字符时输入字母。 I've attached an example below. 我在下面附上一个例子。

UPDATE: Added example code to update the dashes with letters based on word 更新:添加了示例代码以基于单词的字母更新破折号

 var elem = document.getElementById('container'); var guess = document.getElementById('guess'); var word = "Hello"; // draw empty dashes var drawDashes = function(numberOfDashes) { for (var i = 0; i < numberOfDashes; i++) { var el = document.createElement('div'); el.classList = 'dash'; // we draw an empty character inside so that the element // doesn't adjust height when we update the dash later with a // letter inside el.innerHTML = '&nbsp;'; elem.appendChild(el); } } // update dash with a letter based on index var updateDash = function(index, letter) { elem.children[index].innerHTML = letter; } guess.addEventListener('keyup', function(evt) { // split the word up into characters var splitWord = word.split(''); // check to see if the letter entered matches any of the // words characters for (var i = 0; i < splitWord.length; i++ ) { // it is important we convert them to lowercase or // else we might get a mismatch because of case-sensitivity if (evt.key.toLowerCase() === splitWord[i].toLowerCase()) { // update dash with letter based on index updateDash(i, evt.key.toLowerCase()); } } // clear out the value this.value = ''; }); drawDashes(word.length); 
 body { font-family: sans-serif; } .dash { height: 50px; width: 50px; margin: 0 10px; display: inline-block; border-bottom: 2px solid black; font-size: 32px; font-weight: bold; text-align: center; } #guess { height: 50px; width: 50px; padding: 0; font-size: 32px; text-align: center; } 
 <div id="container"></div> <h4>Type a letter</h4> <input id="guess" type="text"/> 

Say your word is in some variable named myWord . 假设您的单词在名为myWord变量中。 Get the length of the word by doing: 通过执行以下操作来获取单词的长度:

var myWordLen = myWord.length;

Then you can create HTML elements using Javascript createElement method and appending child elements, information etc as needed. 然后,您可以使用Javascript createElement方法创建HTML元素,并根据需要附加子元素,信息等。 But since you want as many elements as the length of a word, use a loop. 但是,由于您需要与单词长度一样多的元素,因此请使用循环。 Eg: 例如:

for(var i=0; i < myWordLen; i++)
{
    var tr1 = document.createElement("hr");
    var someEle = document.getElementById("someID");
    someEle.appendChild(tr1);
}

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

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