将 switch 语句与 onMouseOver 结合起来显示文本和图像(javascript)
我是 javascript 新手,希望向我的网站添加我认为相对简单的功能(仍在开发中,尚未托管)。不过,众所周知,当涉及到我认为应该简单的事情时,我犯了严重错误。 :)
我添加了一些效果来显示图像,但我现在可以应用这些效果,所以目前这不是问题。
我所拥有的参考资料没有明确地讨论这一点,所以请原谅我可能正在破坏一些代码(尤其是在切换语句中)。
我希望通过悬停(或 onMouseOver)链接(在同一页面上,但不在 div 附近)的操作来在 div 中显示图像,并在图像下方的单独 div 中显示文本框。任何帮助解决这个问题的帮助将不胜感激。
我有四个具有以下 ID 的链接:
#btn_a;
#btn_b;
#btn_c;
#btn_d;
我有四个具有以下 ID 的图像:
#img_a = "img/ima.jpg";
#img_b = "img/imb.jpg";
#img_c = "img/imc.jpg";
#img_d = "img/imd.jpg";
我有四个具有以下 ID 的文本框:
#txt_a = "Aaaa";
#txt_b = "Bbbb";
#txt_c = "Cccc";
#txt_d = "Dddd";
我分配了两个局部变量:
var varTX; // text
var varIM; // images
我的脚本如下:
JAVASCRIPT
function textBox(varTX){
var varTX=new text();
switch (varTX)
{
case 1: btn_a.onMouseOver: var varTX = "txt_a";
break;
case 2: btn_b.onMouseOver: var varTX = "txt_b";
break;
case 3: btn_c.onMouseOver: var varTX = "txt_c";
break;
case 4: btn_d.onMouseOver: var varTX = "txt_d";
break;
default: varTX = "no text";
}
function textBox(varIM){
var varIM=new image();
switch (varIM)
{
case 1: btn_a.onMouseOver: var varIM = "img_a";
break;
case 2: btn_b.onMouseOver: var varIM = "img_b";
break;
case 3: btn_c.onMouseOver: var varIM = "img_c";
break;
case 4: btn_d.onMouseOver: var varIM = "img_d";
break;
default: varIM = "no image";
}
HTML
<html>
<head>
<script type="text/javascript"></script>
</head>
<body>
<div id="target"><ul>
<li><a href="#" id="btn_a">test_a</a></li>
<li><a href="#" id="btn_b">test_b</a></li>
<li><a href="#" id="btn_c">test_c</a></li>
<li><a href="#" id="btn_d">test_d</a></li>
</ul></div>
<div class="images">
<a href="#">
<img src=<script>textBox("varIM")</script>
width="100%" height="30%" class="latest_img" /></a>
</div>
<div class="images">
<a href="#">
<h3 width="100%" height="30%" class="latest_img" />
<script>textBox("varTX")</script>
</h3></span></a>
</div>
</body>
</html>
I'm new to javascript and am looking to add what I think is a relatively simple function to my website (still being worked on and not yet hosted). I've been known to be horribly wrong when it comes to what I think should be simple, though. :)
I have effects I'm adding to display the image, but I can apply those now, so that's not currently a problem.
The reference materials I have don't go into this explicitly, so please pardon the fact that I'm probably butchering some of the code (especially that in the switching statement).
I would like the action of hovering (or onMouseOver) of a link (on the same page, but not near the divs) to display an image in a div and a text box in a separate div below the image. Any help in straightening this out would be appreciated.
I have four links with the following IDs:
#btn_a;
#btn_b;
#btn_c;
#btn_d;
I have four images with the following IDs:
#img_a = "img/ima.jpg";
#img_b = "img/imb.jpg";
#img_c = "img/imc.jpg";
#img_d = "img/imd.jpg";
I have four text boxes with the following IDs:
#txt_a = "Aaaa";
#txt_b = "Bbbb";
#txt_c = "Cccc";
#txt_d = "Dddd";
I have assigned two local variables:
var varTX; // text
var varIM; // images
The script I have is as follows:
THE JAVASCRIPT
function textBox(varTX){
var varTX=new text();
switch (varTX)
{
case 1: btn_a.onMouseOver: var varTX = "txt_a";
break;
case 2: btn_b.onMouseOver: var varTX = "txt_b";
break;
case 3: btn_c.onMouseOver: var varTX = "txt_c";
break;
case 4: btn_d.onMouseOver: var varTX = "txt_d";
break;
default: varTX = "no text";
}
function textBox(varIM){
var varIM=new image();
switch (varIM)
{
case 1: btn_a.onMouseOver: var varIM = "img_a";
break;
case 2: btn_b.onMouseOver: var varIM = "img_b";
break;
case 3: btn_c.onMouseOver: var varIM = "img_c";
break;
case 4: btn_d.onMouseOver: var varIM = "img_d";
break;
default: varIM = "no image";
}
THE HTML
<html>
<head>
<script type="text/javascript"></script>
</head>
<body>
<div id="target"><ul>
<li><a href="#" id="btn_a">test_a</a></li>
<li><a href="#" id="btn_b">test_b</a></li>
<li><a href="#" id="btn_c">test_c</a></li>
<li><a href="#" id="btn_d">test_d</a></li>
</ul></div>
<div class="images">
<a href="#">
<img src=<script>textBox("varIM")</script>
width="100%" height="30%" class="latest_img" /></a>
</div>
<div class="images">
<a href="#">
<h3 width="100%" height="30%" class="latest_img" />
<script>textBox("varTX")</script>
</h3></span></a>
</div>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里有几个问题。
1) 这是无效的。您无法内联
标记。
2) 不能在 switch 语句中打开两个值,只能打开一个。这是错误的:
3) 不能有两个具有相同 ID 的元素。这是无效的标记。
如果您仍然遇到问题,请修复这些问题并发布另一个问题。
There are several things wrong here.
1) This is invalid. You can't inline a
<script>
tag.2) You can't switch on two values in a switch statement, only one. This is wrong:
3) You can't have two elements with the same ID. That's invalid markup.
Fix these problems and post another question if you're still having trouble.
正如其他人指出的那样,代码存在一些问题。我尝试让基本功能正常工作并稍微清理一下标记。您可以使用 javascript 或 jQuery 来完成此操作
这是一种纯 js 方法。它非常不完整,因此您必须对其进行处理,但这应该可以让您大致了解应该做什么。
这是一种 jQuery 方法。同样不完整,这需要工作。
如果您遇到困难或需要帮助,请随时向我们寻求帮助。 :)
As Others have pointed out there are a few issues with the code. I've tried to get the basic functionality working and clean up the markup a little. You can accomplish this with either javascript or jQuery
This is a pure js way to do it. It's very incomplete so you'll have to work on it but this should give you a general idea of what you should be doing.
This is a jQuery way of doing it. Also incomplete, this will need work.
If you get stuck or need help along the way don't hesitate to ask us for help. :)
也许这会让你开始,扔掉开关并使用 jquery:
和 jquery:
查看它的实际效果: http ://jsfiddle.net/MarkSchultheiss/zah9X/
perhaps this will get you started, tossing out the switch and use the jquery:
and the jquery:
See it in action: http://jsfiddle.net/MarkSchultheiss/zah9X/