Javascript 字检查,带分区号检测?
我试图让我正在研究的随机化程序项目能够检测出出现的单词,根据这个单词,答案会有所不同。例如,如果:Javascript 字检查,带分区号检测?,javascript,jquery,html,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
<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>
随机化
单击此按钮可生成一个随机单词!
您的话会传到这里;点击随机按钮
然后,我希望答案更改为:(请记住,答案是随机的,默认设置为ANSWER#)
随机化
单击此按钮可生成一个随机单词!
单词1
答复1
答复2
答案3
答复4
答复5
答案6
答复7
答复8
答复9
(请记住,上面的代码是当前的&正在工作的功能代码),我正在尝试编写一个代码,该代码捕获div元素,同时尝试将所有应答div的id设置为“output”,而不是后面有数字。所以这是我试图写的代码,但失败得很惨
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"
}
}
}
函数校验字(){
answers=[document.getElementById(“output[i]”)innerHTML];
word=document.getElementById(“outputNumber”).innerHTML;
var i;
对于(i=0;i
例如,我没有完成它;我怎样才能做到这一点?抱歉的小细节帖子;我当时正忙着写这封信。如果您需要更多信息,请发表评论。下面是生成这些内容的全部代码
/*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();})
/*函数播放声音(){
}
*/
/*本节为第一个工具;随机发生器*/
/*这是获取随机数的新函数;也用于getRandomAnswer()函数。要选择不同的数字,请调整范围*/
函数getRandom(){
var nums=[1,2,3,4,5,6,7,8,9];
var gen_nums=[];
_数组中的函数(数组,el){
对于(var i=0;i/*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();})