我的网站中的 javascript 有什么问题?

发布于 2024-12-10 02:18:58 字数 2667 浏览 1 评论 0原文

有人可以解释为什么我的代码不起作用,我想要实现的是包含三个问题的多项选择测验,其中两个在单击时切换到红色图像,另一个变为绿色并运行 javascript,其中显示包含链接的 div到下一页按钮。

<body id="body">

<div id="background">
<div id="container">


<div id="navigation">
<ul class="blue">
<li><a href="#" title="home"><span>home</span></a></li>
<li><a href="#" title="products" class="current"><span>Quiz</span></a></li>
<li><a href="#" title="blog"><span>Sports</span></a></li>
<li><a href="#" title="contact"><span>Contact</span></a></li>
</ul>
</div>
<div id="content">
<div id="left"><img src="images/badminton.jpg" width="246" height="246" /></div>
<div id="right">
<h1>Question 1:</h1>
<h2>What sport is this used for? </h2>




<script language="javascript"> 
function toggle() {
var ele = document.getElementById("next");

if(ele.style.display == "block") {
        ele.style.display = "none";
    text.innerHTML = "show";
}
else {
    ele.style.display = "block";
    text.innerHTML = "hide";
}
} 

</script>



<div id="next" style="display: none"><IMG id=answer border=0 name=answer alt="" src="images/answer.png" width=290 height=60></div>  

</div>
<div id="bottom">

<div id="question"1>
<div id="leftq">
Tennis
</div>
<div id="rightq">

<A onclick="document.answer.src='images/false.png'" href="#" ><IMG id=answer border=0 name=answer alt="" src="images/answer.png" width=290 height=60>
</A> 

</div>

</div>

<div id="question"2>
<div id="leftq">
Squash</div>
<div id="rightq">

<A onclick="document.answer.src='images/false.png'" href="#" ><IMG id=answer border=0 name=answer alt="" src="images/answer.png" width=290 height=60>
</A> 

</div>
</div>

<div id="question"3>
<div id="leftq">
Badminton</div>
<div id="rightqcorrect">

<A onclick="document.answer.src='images/true.png'" href="javascript:toggle();" ><IMG id=answer border=0 name=answer alt="" src="images/answer.png" width=290 height=60>
</A> 

</div>
</div>

</div>
</div>
</div>
</div>
</body>

我知道我的代码有点乱...抱歉

arran-15 岁的网络编码员

编辑- 我已将其放入我的保管箱中,以便您可以看到我的问题。 点击此处 我想做的是,当您单击网球或壁球旁边的图像时,它会变成红色(false.png),当他们单击羽毛球时,它会变成绿色(true.png),然后出现下一个问题,在此刻只出现下一个问题,并且我在图像更改方面遇到问题

Can someone explain why my code isn't working, what i want to achieve is a multiple choice quiz with three questions, two of them switch to a red image when clicked, the other goes green and runs javascript which shows a div containing the link to the next page button.

<body id="body">

<div id="background">
<div id="container">


<div id="navigation">
<ul class="blue">
<li><a href="#" title="home"><span>home</span></a></li>
<li><a href="#" title="products" class="current"><span>Quiz</span></a></li>
<li><a href="#" title="blog"><span>Sports</span></a></li>
<li><a href="#" title="contact"><span>Contact</span></a></li>
</ul>
</div>
<div id="content">
<div id="left"><img src="images/badminton.jpg" width="246" height="246" /></div>
<div id="right">
<h1>Question 1:</h1>
<h2>What sport is this used for? </h2>




<script language="javascript"> 
function toggle() {
var ele = document.getElementById("next");

if(ele.style.display == "block") {
        ele.style.display = "none";
    text.innerHTML = "show";
}
else {
    ele.style.display = "block";
    text.innerHTML = "hide";
}
} 

</script>



<div id="next" style="display: none"><IMG id=answer border=0 name=answer alt="" src="images/answer.png" width=290 height=60></div>  

</div>
<div id="bottom">

<div id="question"1>
<div id="leftq">
Tennis
</div>
<div id="rightq">

<A onclick="document.answer.src='images/false.png'" href="#" ><IMG id=answer border=0 name=answer alt="" src="images/answer.png" width=290 height=60>
</A> 

</div>

</div>

<div id="question"2>
<div id="leftq">
Squash</div>
<div id="rightq">

<A onclick="document.answer.src='images/false.png'" href="#" ><IMG id=answer border=0 name=answer alt="" src="images/answer.png" width=290 height=60>
</A> 

</div>
</div>

<div id="question"3>
<div id="leftq">
Badminton</div>
<div id="rightqcorrect">

<A onclick="document.answer.src='images/true.png'" href="javascript:toggle();" ><IMG id=answer border=0 name=answer alt="" src="images/answer.png" width=290 height=60>
</A> 

</div>
</div>

</div>
</div>
</div>
</div>
</body>

i know my code is a bit messy...sorry

arran-15 year old web coder

Edit-
i have placed it in my dropbox so you can see my problem.
Click Here
what i am trying to do is, when you click the image next to tennis or squash it turns red (false.png) and when they click badminton it turns green (true.png), which then makes the next question appear, at the moment only the next question appears and i'm having trouble with the images changing

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

獨角戲 2024-12-17 02:18:58

text 未定义。您必须先定义变量,然后才能使用它。

由于您使用的是 .innerHTML = ...,我假设 text 必须以这种方式声明:

JS:

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

HTML (somewhere):

<div id="text"></div>

text is undefined. You have to define the variable, before you can use it.

Since you're using .innerHTML = ..., I assume that text has to be declared in such way:

JS:

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

HTML (somewhere):

<div id="text"></div>
童话里做英雄 2024-12-17 02:18:58

将脚本添加到 There are much Missing 的末尾

之后重试。

Add the script to the end of the

There are many </div> missing . <script> cannot be inside a <div>

<script> should be below <div id='next'>

After this try again .

ゃ人海孤独症 2024-12-17 02:18:58

你的 JavaScript 在语法上是没问题的,尽管它没有做你想要它做的事情。我对你的 HTML 确实有一些建议。请确保:

  • 元素 ID 是唯一的。 (即只有一个具有 id 答案的元素)作为
  • 字符串的元素属性值用引号引起来。 (即
  • 不要使用 document.answer,而是使用 document.getElementById("answer")

我将更新此答案,并提供一些有关如何修复您的问题的建议JavaScript 很快。

更新:这是您要查找的内容的简化版本。随意修改:http://jsfiddle.net/U7J5W/

Your JavaScript is syntactically okay, although it isn't doing what you want it to do. I do have some advice about your HTML. Please make sure:

  • Element ids are unique. (ie. only one element with id answer)
  • Element attribute values that are strings are in quotes. (ie. <div id="question1"/>)
  • Instead of document.answer, use document.getElementById("answer")

I will update this answer with some advice about how to fix your JavaScript soon.

Update: Here's a simplified version of what you're looking for. Feel free to modify: http://jsfiddle.net/U7J5W/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文