2014-12-04 94 views
-1

通知

我已经在我的alt帐户上提出过这个问题David Vex;但是该帐户被激怒了,我无法登录到它,一个StackOverflow服务器错误与胡言乱语谈论错误:0x12084123其次是服务器乱码;所以跟进它的唯一方法就是对它进行调整。请原谅不便。从问题Tab索引JavaScript函数未知/未指定错误?


报价(Alt键账户)

可加工CODE

Better than JSFiddle!

我试图做一个表格,表格中的onClick其中,它会激活每一个元素的tabindex imageSelector功能(未命名)。我从我最后一个问题中得到了代码,这个代码没有命名函数。它适用于'alert'变体,但我适合于我需要检查答案的函数,如果if(answer1.innerHTML ==“Correct Answer”){document.getElementById(“correctAnswer”)。addAttribute (“display”,“inline”)}是活动的,它会知道答案是正确的答案,并且会设置图片的id =“correctAnswer”来显示,但是3秒后它应该回到显示=“隐藏”并重新激活整个随机序列,如果按钮还没有被选中,这似乎不起作用。我尝试使用setTimeout()函数在答案正确/不正确时使用setTimeout()函数,它会设置一个延迟来调用将使图像不可见并重新随机化答案的函数。我将显示代码,并在代码之后重新解释每个部分。

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!</p> 
     <tr> 
      <td class="output" id="output1" tabindex="1"></td> 
      <td class="output" id="output2" tabindex="1"></td> 
      <td class="output" id="output3" tabindex="1"></td> 
     </tr> 
     <tr> 
      <td class="output" id="output4" tabindex="1"></td> 
      <td class="output" id="output5" tabindex="1"></td> 
      <td class="output" id="output6" tabindex="1"></td> 
     </tr> 
     <tr> 
      <td class="output" id="output7" tabindex="1"></td> 
      <td class="output" id="output8" tabindex="1"></td> 
      <td class="output" id="output9" tabindex="1"></td> 
     </tr> 
     </table> 
    </div> 
    <div id="checkAnswer"> 
    <img id="correctAnswer" src="http://png-1.findicons.com/files/icons/1965/colorcons_smoke/128/checkmark.png" alt="correct" style="position: absolute; left: 100px; display: none;"> 
    <img id="incorrectAnswer" src="http://png-4.findicons.com/files/icons/1008/quiet/128/no.png" alt="incorrect" style="position: absolute; right: 100px; display: none;"> 
    </div> 
    </div> 

这勾画出整个序列。 outputNumber是数字将生成然后转换为单词的位置。按钮div很简单;它是按钮的位置。答案div保存表格,并且每个元素都与targetting的id匹配,tabindex用于使其可点击。 checkAnswer div保存两个隐藏的图像。

CSS

不是很重要;它包含所有的Daneden的animate.css的代码(3150线),加上10多个线的页面的着色...

的JavaScript

/* Has the words and their respectful answers. */ 
var words = [ 
    { word: "Fruits A-B", array: ["Apple", "Apricot", "Avacado", "Banana", "Breadfruit", "Bilberry", "Blackberry", "Blackcurrant", "Blueberry"] }, 
    { word: "Fruits B-C", array: ["Boysenberry", "Cantaloupe", "Currant", "Cherry", "Cherimoya", "Cloudberry", "Coconut", "Cranberry", "Cucumber"] }, 
    { word: "Fruits D-G", array: ["Damson", "Date", "Dragonfruit", "Durian", "Eggplant", "Elderberry", "Feijoa", "Fig", "Goji berry"] }, 
    { word: "Fruits G-K", array: ["Gooseberry", "Grape", "Grapefruit", "Guava", "Huckleberry", "Honeydew", "Jackfruit", "Jambul", "Kiwi fruit"] }, 
    { word: "Fruits K-M", array: ["Kumquat", "Lemon", "Lime", "Loquat", "Lychee", "Mango", "Marion berry", "Melon", "Miracle fruit"] }, 
    { word: "Fruits M-P", array: ["Mulberry", "Nectarine", "Nut", "Olive", "Orange", "Papaya", "Passionfruit", "Peach", "Pepper"] }, 
    { word: "Fruits P-Q", array: ["Pear", "Persimmon", "Physalis", "Plum", "Pineapple", "Pomegranate", "Pomelo", "Purple Mangosteen", "Quince"] }, 
    { word: "Fruits R-T", array: ["Raspberry", "Rambutan", "Salal berry", "Salmon berry", "Satsuma", "Star fruit", "Strawberry", "Tomarillo", "Tomato"] }, 
    { word: "Fruits U-Z", array: ["Ugli fruit", "Watermelon", "Bell pepper", "Chili pepper", "Clementine", "Mandarine", "Tangerine", "Blood Orange", "Rock Melon"] } 
]; 
/* This function grabs the word that is outputted, then changes the answers based on that word. Change to your liking! */ 
function grabWord() { 
    var word = document.getElementById("outputNumber").innerHTML; 
    var wordIndex; 
    for (var i = 0; i < words.length; i++) { 
    if (words[i].word === word) { 
     wordIndex = i; 
     break; 
    } 
    } 
    for (var i = 1; i <= 9; i++) { 
    document.getElementById("output" + i).innerHTML = words[wordIndex].array[i-1]; 
    } 
} 
/* This function SHOULD be working, which it does if the function is something like alert(message) but with the function I need for the image visibility and such, it doesn't work; it doesn't even give me an answer. */ 
var cells = document.getElementsByTagName("td"); 
for (var i = 0; i < cells.length; i++) { 
    cells[i].addEventListener("click", function() { 
    var word = document.getElementById("outputNumber").innerHTML; 
    var answer1 = document.getElementById("output1"); 
    var answer2 = document.getElementById("output2"); 
    var answer3 = document.getElementById("output3"); 
    var answer4 = document.getElementById("output4"); 
    var answer5 = document.getElementById("output5"); 
    var answer6 = document.getElementById("output6"); 
    var answer7 = document.getElementById("output7"); 
    var answer8 = document.getElementById("output8"); 
    var answer9 = document.getElementById("output9"); 
    if(word == "Fruits U-Z") { 
     if(answer1.innerHTML == "Ugli Fruit") { 
     document.getElementById("correctAnswer").setAttribute("display", "inline") 
     } 
     else { 
     document.getElementById("incorrectAnswer").setAttribute("display", "inline") 
     } 
    } 
    }) 
} 

我把它浓缩尽可能多地,但对于grabWord()函数,我必须保持它很长,以便每个单词可以手动更改答案。它被设置为现在的例子。

错误/问题

当我点击,将其是否正确与否,它确实没有什么会检查的最后一部分匹配的答案。所以我检查开发控制台(F12在浏览器中)并查看没有错误。

任何想法?

,请记住

我通常不善于包括细节/信息。如果您需要更多详细信息,请点评,我将尽可能多地添加信息。

+0

尝试减少所有代码到**只是**复制您的错误。 1)你可能会在找到解决方案的同时做到这一点。 2)它可以帮助我们缩小问题范围,而不必处理整个页面的所有分散注意力。越简洁,你可以提出你的问题,你可能得到的回应越多。干杯! – philtune 2014-12-04 15:21:28

+0

@philtune固定 – 2014-12-04 15:27:31

+0

为什么jQuery标签? – j08691 2014-12-04 15:29:35

回答

2

看看你的代码...它正在工作。但是,您将属性“display”设置为“inline”;如果您检查元素的正确或不正确答案,则不在下面的样式...调整中。

此外,在水果UZ上只有正确或不正确的答案,并且没有正确的答案......在这种情况下,您将数组中的“Ugli水果”与“Ugli Fruit”作为字符串进行比较。

var cells = document.getElementsByTagName("td"); 
for (var i = 0; i < cells.length; i++) { 
    cells[i].addEventListener("click", function() { 
    var word = document.getElementById("outputNumber").innerHTML; 
    var answer1 = document.getElementById("output1"); 
    var answer2 = document.getElementById("output2"); 
    var answer3 = document.getElementById("output3"); 
    var answer4 = document.getElementById("output4"); 
    var answer5 = document.getElementById("output5"); 
    var answer6 = document.getElementById("output6"); 
    var answer7 = document.getElementById("output7"); 
    var answer8 = document.getElementById("output8"); 
    var answer9 = document.getElementById("output9"); 
    console.log(word, answer1.innerHTML); 
    if(word == "Fruits U-Z") { 
     if(answer1.innerHTML == "Ugli Fruit") { 
     document.getElementById("correctAnswer").setAttribute("style", "display:inline; position:absolute; left:100px;"); 
     document.getElementById("incorrectAnswer").setAttribute("style", "display:none; position:absolute; right:100px;"); 
     } 
     else { 
       document.getElementById("correctAnswer").setAttribute("style", "display:none; position:absolute; left:100px;"); 
document.getElementById("incorrectAnswer").setAttribute("style", "display:inline; position:absolute; right:100px;"); 
     } 
    } 
    }); 
}