简体   繁体   English

停止使用Javascript在hangman游戏中包装字母

[英]Stop letters from wrapping in hangman game using Javascript

I have made a hangman game using JavaScript. 我使用JavaScript制作了a子手游戏。 It works the way you would expect, with correct letters replacing the blank "_" that represents the letters. 它以您期望的方式工作,用正确的字母替换代表字母的空白“ _”。 The problem is larger answers on smaller screens are wrapping, which is fine and what I want, but I want the WHOLE WORD to wrap if it doesn't fit, but at the moment, only individual letters wrap. 问题是较大的答案可以在较小的屏幕上自动换行,这很好,我想要什么,但是我希望在不适合的情况下自动换行,但目前仅单个字母可以换行。

So for example something like "Tropic Thunder" is appearing like this: 例如,类似“ Tropic Thunder”的出现如下:

"Tropic Thun “热带图恩

der" der”

Rather than what I want, which is: 而不是我想要的是:

"Tropic “热带

Thunder" 雷”

If the word doesn't fit then I want the whole word to wrap and drop under, not just the letters that don't fit. 如果该单词不合适,那么我希望整个单词都可以自动换行并放下,而不仅仅是不合适的字母。

I think my JavaScript code doesn't differentiate different words from the same answer. 我认为我的JavaScript代码无法将不同的单词与同一个答案区分开。

For some reason this text box isn't putting all my code into a block, so to make things easier to read, this is a link to my code in github: 出于某种原因,此文本框未将我的所有代码放入一个块中,因此为了使内容更易于阅读,这是指向github中我的代码的链接:

HTML: HTML:

https://raw.githubusercontent.com/kaw31/hangman/master/hangman.html https://raw.githubusercontent.com/kaw31/hangman/master/hangman.html

CSS: (I've commented out a bit of code from a previous version so just ignore that); CSS :(我已经注释掉了先前版本中的一些代码,因此请忽略它);

https://raw.githubusercontent.com/kaw31/hangman/master/css/style.css https://raw.githubusercontent.com/kaw31/hangman/master/css/style.css

JavaScript: JavaScript:

https://raw.githubusercontent.com/kaw31/hangman/master/js/script.js https://raw.githubusercontent.com/kaw31/hangman/master/js/script.js

Any help is much appreciated. 任何帮助深表感谢。

It looks like your code inserts a new <li> element into the DOM for every character of the answer (at guess = document.createElement('li') ). 看起来您的代码为答案的每个字符都在DOM中插入了一个新的<li>元素( guess = document.createElement('li') )。 So, the markup will look like this: 因此,标记将如下所示:

<li>f</li><li>o</li><li>o</li><li> </li><li>b</li><li>a</li><li>r</li>

Then, those <li> elements are displayed as inline-block in your CSS (at #my-word li ). 然后,这些<li>元素在CSS中显示为inline-block (位于#my-word li )。 Set the CSS display property to inline instead, and the words will break as you expect. 将CSS display属性设置为inline ,单词将按预期中断。

I think what you are looking for is the css property word-wrap: break-word; 我认为您正在寻找的是CSS属性word-wrap: break-word; This should bump the whole word down a line. 这应该使整个单词沿直线下降。

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

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