【问题标题】:Switch cases with onmouseover and onmouseout events使用 onmouseover 和 onmouseout 事件切换案例
【发布时间】:2017-01-20 07:04:21
【问题描述】:

我一直在尝试让开关同时使用 onmouseover 和 onmouseout,但我不知道如何将它绑定到事件。提前感谢您的帮助

<script>
function img(img){
switch(img)
{
case "showSq": 
document.getElementById("square").innerHTML = '<img src="square.png">';
break;
case "hideSq": 
document.getElementById("square").innerHTML = '';
break;
case "showCr": 
document.getElementById("circle").innerHTML = '<img src="circle.png">';
break;
case "hideCr": 
document.getElementById("circle").innerHTML = '';
break;
}}
</script> 
<body>
<div id=right>
<h1> Geometry </h1>
<p>
<span onmouseover="img(showSq)" onmouseout="img(hideSq)"> SQUARE</span>,
<span onmouseover="img(showCr)" onmouseout="img(hideCr)"> CIRCLE </span>
</p>
<p> <span id="circle"> <span id="square"> </p>
</div>

【问题讨论】:

  • 你的参数应该是作为静态字符串引号内的 img('showSq') 值
  • 谢谢,已修复,但仍然无法正常工作。
  • 我刚刚测试了它,它可以工作!
  • 请问您使用的是哪个浏览器?我真的无法让它在 chrome 中工作。
  • 我使用的是谷歌浏览器

标签: javascript switch-statement case onmouseover onmouseout


【解决方案1】:

您的参数应为 img('showSq') 和 &lt;span&gt; 标签缺少 &lt;/span&gt; 标签

<p> <span id="circle"></span> <span id="square"></span> </p>

【讨论】:

  • 有 标记,但在参数中添加 '' 只会使圆圈出于某种原因工作
  • 那么它可能是错字或丢失的图像文件。你能创造一个小提琴或钢笔吗?
  • 似乎圆形压倒了方形。当我在正方形上运行鼠标时,它可以工作,但是在我尝试使用圆形后,正方形停止做任何事情。
【解决方案2】:

添加单庭制作一串函数参数

<span onmouseover="img('showSq')" onmouseout="img('hideSq')"> SQUARE</span>,
<span onmouseover="img('showCr')" onmouseout="img('hideCr')"> CIRCLE </span>

【讨论】:

    猜你喜欢
    • 2011-04-06
    • 2012-09-21
    • 2023-03-03
    • 1970-01-01
    • 2015-06-22
    • 2012-02-04
    • 2015-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多