带有Div编号检测的字检查?

时间:2014-11-24 17:48:02

标签: javascript jquery html

我试图让我正在研究的随机发生器项目能够检测出现在的单词,并且基于该单词,答案会有所不同。例如,如果:

  <div id="randomizer">
    <div id="wordOutput">
      <div id="button">
        <!-- This is the button that calls the getRandom() function to create the word. --><button id="myBtn">Randomize!</button><br>
        <caption>Click this button to generate a random word!
        </caption>
        <!-- This is apart of the Randomizer tool, which can be changed to fit the words. It will output the answers based on  -->
        </button>
      </div>
    </div>
    <div id="answers" class="answers">
      <table>
        <p id="outputNumber" class="outputNumber">Your word will go here; Click the Randomize Button! <!-- LETS SAY THIS OUTPUTS TO "Word 1" --></p>
        <div class="output" id="output1"></div>
        <div class="output" id="output2"></div>
        <div class="output" id="output3"></div><br>
        <div class="output" id="output4"></div>
        <div class="output" id="output5"></div>
        <div class="output" id="output6"></div><br>
        <div class="output" id="output7"></div>
        <div class="output" id="output8"></div>
        <div class="output" id="output9"></div>
      </table>
    </div>
  </div>

然后我希望将答案改为:(保持答案,答案是通过默认设置为答案#)

  <div id="randomizer">
    <div id="wordOutput">
      <div id="button">
        <!-- This is the button that calls the getRandom() function to create the word. --><button id="myBtn">Randomize!</button><br>
        <caption>Click this button to generate a random word!
        </caption>
        <!-- This is apart of the Randomizer tool, which can be changed to fit the words. It will output the answers based on  -->
        </button>
      </div>
    </div>
    <div id="answers" class="answers">
      <table>
        <p id="outputNumber" class="outputNumber">Word 1</p>
        <div class="output" id="output1">Answer 1 <!-- becomes "Apple"--></div>
        <div class="output" id="output2">Answer 2 <!-- becomes "Banana"--></div>
        <div class="output" id="output3">Answer 3 <!-- becomes "Rock"--></div><br>
        <div class="output" id="output4">Answer 4 <!-- becomes "Stone"--></div>
        <div class="output" id="output5">Answer 5 <!-- becomes "Milk"--></div>
        <div class="output" id="output6">Answer 6 <!-- becomes "Juice"--></div><br>
        <div class="output" id="output7">Answer 7 <!-- becomes "Grapes"--></div>
        <div class="output" id="output8">Answer 8 <!-- becomes "Tomato"--></div>
        <div class="output" id="output9">Answer 9 <!-- becomes "All of the Above"--></div>
      </table>
    </div>
  </div>

(保持上面的代码是当前和工作功能代码)并且我试图在尝试获取所有答案div的ID时尝试抓取div元素的代码设置为&#34;输出&#34;而不是后面的数字。所以这是我试图编写的代码,但却失败了......

function checkWord()  {
  answers = [document.getElementById("output[i]").innerHTML];
  word = document.getElementById("outputNumber").innerHTML;
  var i;
  for (i = 0; i < answers.length; i++)  {
    if (word == "Word 1")  {
      answers[0] = "Possible Answer 1"
    }
    else {
      word = "Error Answer :c"
    }
  }
}

我没有以示例目的完成它;我该怎么做才能做到这一点?对不起,小细节的帖子;我急着写这篇文章。如果您需要更多信息,请发表评论。这是产生这些东西的整个代码。

/*function playSound()  {

}
*/
/* This section is for the first tool; the Randomizer.*/
/* This is the new function for getting a random number; also used in the getRandomAnswer() function. To select different numbers, adjust the range. */
function getRandom() {
  var nums = [1,2,3,4,5,6,7,8,9];
  var gen_nums = [];

  function in_array(array, el) {
    for(var i = 0 ; i < array.length; i++) 
      if(array[i] == el) return true;
    return false;
  }

  function get_rand(array) {
    var rand = array[Math.floor(Math.random()*array.length)];
    if(!in_array(gen_nums, rand)) {
      gen_nums.push(rand); 
      return rand;
    }
    return get_rand(array);
  }

  for(var i = 0; i < 9; i++) {
    return (get_rand(nums));
  }
}
function timeOut(){
  /* This can be ignored, as it was a testing function for creating, making, and fixing the randomizer tool, but may change based on creating new tools that need to be troubleshooted. */
  setTimeout (changeRandom, 1);
}
/* This is the function that grabs the innerHTML (what the box says) of the #output1 and changes that number to the word specified. [In example, if the number [randomly] generated was 5, then this function detects that the number was 5 and changes it to Word 5.]  */
function changeRandom()  {
  /* Using the "var x" command, this tells the function that whenever there's an x in the code, it will read it as "document.getElementById('output1').innerHTML", and will grab whatever is inside that #output1 element. */
  var x = document.getElementById('outputNumber').innerHTML
  /* Here's the tutorial for modifying this tool for the possible answers. This tool [by default] has it set to  */
  if(x == 1)  {
    document.getElementById("outputNumber").innerHTML = "Word 1";
  } else if(x == 2)  {
    document.getElementById("outputNumber").innerHTML = "Word 2";
  } else if(x == 3)  {
    document.getElementById("outputNumber").innerHTML = "Word 3";
  } else if(x == 4)  {
    document.getElementById("outputNumber").innerHTML = "Word 4" 
  } else if(x == 5) {
    document.getElementById("outputNumber").innerHTML = "Word 5"
  }
  else if(x == 6) {
    document.getElementById("outputNumber").innerHTML = "Word 6"
  } 
  else if(x == 7) {
    document.getElementById("outputNumber").innerHTML = "Word 7"
  }
  else if(x == 8) {
    document.getElementById("outputNumber").innerHTML = "Word 8"
  }
  else if(x == 9) {
    document.getElementById("outputNumber").innerHTML = "Word 9"
  }
  else if(x == 10) {
    document.getElementById("outputNumber").innerHTML = "Word 10"
  }
  /* If the function is broken [by user modifications], it will output "Error :c", which means the user should check their modifications. */
  else  {
    document.getElementById("output1").innerHTML = "Error :c"
  }
}
document.getElementById("myBtn").addEventListener("click", function(){document.getElementById("outputNumber").innerHTML = getRandom(); changeRandom();})
function testFunction()  {
  if(document.getElementById("output1").innerHTML == 3)  {
    document.getElementById("output1").innerHTML = "Three";
  }
  else  {
    document.getElementById("output1").innerHTML = "N3"
  }
}
function getRandomAnswer() {
  var nums = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  var copy = nums.slice();

  for (var i = 0, len = nums.length; i < len; i++) {
    var j = Math.floor(Math.random() * copy.length);
    var rand = copy[ j ];

    // remove from array
    copy.splice(j, 1);

    // add to output
    document.getElementById('output' + (i + 1)).innerHTML = rand;
  }
  //return gen_nums;
  //document.getElementById('output' + (i + 1)).innerHTML = getRandomAnswer();
}
function changeRandomAnswer()  {

  var answers = document.getElementById("answers");

  for(var i = 0, len = answers.children.length; i < len; i++) {
    var output = answers.children[i];
    var answer = output.innerHTML;

    if(answer == "1")  {
      output.innerHTML = "Answer 1";
    }
    else if(answer == "2")  {
      output.innerHTML = "Answer 2";
    } 
    else if(answer == "3")  {
      output.innerHTML = "Answer 3";
    } 
    else if(answer == "4")  {
      output.innerHTML = "Answer 4" 
    } 
    else if(answer == "5") {
      output.innerHTML = "Answer 5"
    }
    else if(answer == "6") {
      output.innerHTML = "Answer 6"
    } 
    else if(answer == "7") {
      output.innerHTML = "Answer 7"
    }
    else if(answer == "8") {
      output.innerHTML = "Answer 8"
    }
    else if(answer == "9") {
      output.innerHTML = "Answer 9"
    }
  }

}
function checkWord()  {
  answers = [document.getElementById("output[i]").innerHTML];
  word = document.getElementById("outputNumber").innerHTML;
  var i;
  for (i = 0; i < answers.length; i++)  {
    if (word == "Word 1")  {
      answers[0] = "Possible Answer 1"
    }
    else {
      word = "Error Answer :c"
    }
  }
}
document.getElementById("myBtn").addEventListener("click", function(){ getRandomAnswer(); changeRandomAnswer();})

0 个答案:

没有答案