当if成为页面时,只会冻结js

时间:2018-01-06 18:53:21

标签: javascript jquery

所以我有这个功能,当if语句完成时,它除了冻结页面之外什么都不做。

    function doGetWord(){



            word = F.gword.value;
            if (word == "") {
                $("input").hide();
                $("input.wordi").show();
                $("input.bi").show();
            }
            else{
                wLength = word.length;
                cs = ++cs % 2
                for(var i = 0; i < wLength; i++){
                    document.getElementById("dword").innerHTML += "_ "
                }
                $("input.wordi").hide()
                $("input.bi").hide()
                $("input.in2").show()
                $("input.bi2").show()
            }

        }

编辑1 - 我编辑了将我的代码放在那里直到该功能的帖子,如果这对你不起作用我不知道会发生什么。 请花一点时间阅读它并告诉我它有什么问题。 .................................................. .................................................. .................................................. ..........

&#13;
&#13;
var word = "" //word variable
			var wLength = 0 //word length variable
			var dummy = "" //letter guessed
			var player1 = ""
			var player2 = ""
			var score1 = 0;
			var score2 = 0;
			var cs = 0; //which one is receiving points?
			var cs2 = 0;
			var dm = new Array(26)
			var rou = 0; //current round
			var alp = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
			var corrects = 0;
			var tries = 7;


			$(document).ready(function(){
				$("input.in2").hide()
				$("input.bi2").hide()
				$("input.wordi").hide()
				$("input.bi").hide()
				$("div#chrts").hide()
			})

			function doSetPlayersName(){
				player1 = F.pl1.value
				player2 = F.pl2.value
				if (player1 == "") {
					do {
						player1 = prompt("Please Choose a proper name for the first player.")
					} while (player1 == "");
				}

				if (player2 == "") {
					do {
						player2 = prompt("Please Choose a proper name for the second player.")
					} while (player2 == "");
				}

				$("input.wordi").show()
				$("input.bi").show()
				$("input.bi3").hide()
				$("input.in3").hide()
				$("input.in4").hide()
				$("span.plc1").hide()
				$("span.plc2").hide()
				rou++
				document.getElementById("round").textContent = "Round " + rou
				document.getElementById("playerguessing").innerHTML = "The Player " + player1 + " is choosing the word.";
			}



			function doGetWord(){



				word = F.gword.value;
				if (word == "") {
					$("input").hide();
					$("input.wordi").show();
					$("input.bi").show();
				}
				else{
					wLength = word.length;
					cs = ++cs % 2
					for(var i = 0; i < wLength; i++){
						document.getElementById("dword").innerHTML += "_ "
					}
					$("input.wordi").hide()
					$("input.bi").hide()
					$("input.in2").show()
					$("input.bi2").show()
				}

			}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="F">
			<div id="round">Round 0</div>

			<table>
				<tr>
					<td><div id="sc1" class="plc1"></div></td>
					<td><div id="sc2" class="plc2"></div></td>
				</tr>
			</table>

			<div class="rules">
				<span style="font-size: 20pt; font-family: Koliko;">Rules:</span> <br>
				One Player will write a word, and the other has 7 chances to guess its letters, but if he guesses a letter wrong he'll lose an amethyst.
				If he loses all of them, he loses the round, and the other player gets 1 point, but if he guesses the word, then he's the one getting
				the point.
			</div>
			<center>
				<div class="con">
					<div id="playerguessing"></div>
					<input type="password" name="gword" class="wordi" placeholder="Write the Word to be guessed">
					<input type="text" name="t" class="in2">
					<span class="plc1">Player 1: </span><p><input type="text" name="pl1" class="in3" placeholder="e.g: Fried Potato"></p>
					<span class="plc2">Player 2: </span><p><input type="text" name="pl2" class="in4" placeholder="e.g: Gragas Urgot"></p>
					<input type="button" name="b3" value="Done" onclick="doSetPlayersName()" class="bi3">
					<div id="dword"></div>
					<input type="button" name="b" value="Choose Word" onclick="doGetWord()" class="bi">
					<input type="button" name="b2" value="do" onclick="doGuess()" class="bi2">
					<div id="chrts"></div>
				</div>
			</center>





		</form>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

在评论中无法得到我想要的东西,所以我会尝试在这里看到我用doGetWord()函数看到的一些明显问题。

  1. F.gword.value未在函数中定义。如果它作为全局存在于其他位置,则放置console.log(F.gword.value);在功能中使用它之前。

  2. 我无法判断F是否曾在任何地方定义过。尝试使用console.log(F)并查看它是否已定义。

  3. 在使用F.gword.value

    之前添加此项
           if(!F.gword.value)
             return;
    

    这应该会停止代码中的异常。您需要在输入中填写名称,以便不要尝试使用未定义的值。

    为undefined添加测试。

    if(word ==“” || word == undefined ){....

    您可能已经知道这一点,但HTML中的doGuess()未在您的代码中定义。

      <center>
    

    是过时/非标准的。应该使用像

    这样的东西
      <div class"center">
    
相关问题