所以我使用javascript来帮助我做一个简单的个性测验。容易吗? 我有一个问题。在最后一个问题上,我得到了一个名为" finalPage"然后我有一个if语句,检查它确实说" finalPage"。奇怪的是它假设它错了。我也试过获取单词长度并将其与数字相匹配但无济于事。这是代码。 我还检查过以确保单词匹配。我不知道是怎么回事。
HTML:
<div id="finalQuestion" data-next="finalPage">
<h2> Question 10, Once you finish do this again! </h2>
<h3> Pick the best Genre </h3>
<div class="button" data-group="Keyboard"> Thriller </div>
<div class="button" data-group="Brass"> Comedy </div>
<div class="button" data-group="Woodwind"> Romance </div>
<div class="button" data-group="Guitar"> Action </div>
<div class="button" data-group="Percussion"> Horror </div>
</div>
</div>
<div id="finalPage">
<div id="GuitarPage">
<h2> Congratulations! You will be turned into a...</h2>
<h1> Guitar Instrument! </h1>
<!-- insert image -->
</div>
<div id="BrassPage">
<h2> Congratulations! You will be turned into a...</h2>
<h1> Brass Instrument! </h1>
<!-- insert image -->
</div>
<div id="WoodwindPage">
<h2> Congratulations! You will be turned into a...</h2>
<h1> Woodwind Instrument! </h1>
<!-- insert image -->
</div>
<div id="PercussionPage">
<h2> Congratulations! You will be turned into a...</h2>
<h1> Percussion Instrument! </h1>
<!-- insert image -->
</div>
<div id="KeyboardPage">
<h2> Congratulations! You will be turned into a...</h2>
<h1> Keyboard Instrument! </h1>
<!-- insert image -->
</div>
</div>
继承人Javascript:
var thispage = this.parentElement;
thispage.style.display = "none";
var nextQuestion= document.getElementById( thispage.dataset.next );
if (nextQuestion == "finalPage")
{
if(Guitar > Percussion && Brass && Keyboard && Woodwind)
{
document.getElementById("Guitar").style.display ="none";
}
if(Percussion > Guitar && Brass && Keyboard && Woodwind)
{
document.getElementById("Percussion").style.display ="none";
}
if(Brass > Percussion && Guitar && Keyboard && Woodwind) {
document.getElementById("Brass").style.display ="none";
}
if(Keyboard > Percussion && Guitar && Brass && Woodwind) {
document.getElementById("Keyboard").style.display ="none";
}
if(Woodwind > Percussion && Guitar && Keyboard && Brass) {
document.getElementById("Woodwind").style.display ="none";
}
//show final page
}
else {
nextQuestion.style.display = "block";
}
}
答案 0 :(得分:1)
getElementById
返回一个DOM元素,而不是它的id。如果你想知道你所抓获的元素是否具有“finalPage”的id,你必须明确检查它:
if (nextQuestion.getAttribute('id') === 'finalPage') {
// ...
}