简体   繁体   English

javascript onmouseover 3个独立div的可见性切换

[英]javascript onmouseover visibility toggle of 3 separate div's

I'm trying to have three image links show three separate div's (respectively) when onMouseOver. 我想在onMouseOver上有三个图像链接显示三个独立的div(分别)。

<script type="text/javascript">
function toggleVisibility(divid) {
if (divid="1"){
    document.getElementById("1b").style.visibility = "visible";
    document.getElementById("2b").style.visibility = "hidden";
    document.getElementById("3b").style.visibility = "hidden";
}
else if (divid="2")
{
    document.getElementById("1b").style.visibility = "hidden";
    document.getElementById("2b").style.visibility = "visible";
    document.getElementById("3b").style.visibility = "hidden";
}
else if (divid="3")
{
    document.getElementById("1b").style.visibility = "hidden";
    document.getElementById("2b").style.visibility = "hidden";
    document.getElementById("3b").style.visibility = "visible";
}
}
</script>

With these onMouseOver events used on all three anchor tags. 在所有三个锚标签上使用这些onMouseOver事件。

onmouseover="toggleVisibility('1');"
onmouseover="toggleVisibility('2');"
onmouseover="toggleVisibility('3');"

HOWEVER, 然而,

All 3, when onMouseOver, show 1a . 所有3,当onMouseOver时,显示1a And that is all it does. 这就是它的全部。

1a does not go hidden when the other 2 are rolled over and 2a + 3a do not show at all. 当其他2个被翻转并且2a + 3a完全没有显示时,1a不会被隐藏。

Thanks 谢谢

HTML + CSS: HTML + CSS:

<div id="wrapper">
<div style="width:910px;height:300px;margin:0;padding:0;">
<div id="1b">&nbsp;</div>
<div id="2b">&nbsp;</div>
<div id="3b">&nbsp;</div>
<a href="#" onmouseover="toggleVisibility('1');" class="1"></a>
<a href="#" onmouseover="toggleVisibility('2');" class="2"></a>
<a href="#" onmouseover="toggleVisibility('3');" class="3"></a>
</div>
</div>

#wrapper {
width: 896px;
margin: 0px auto;
text-align: left;
overflow: hidden;
}

#1b {
width:303px;
height:150px;
visibility:hidden;
float:left;
background-color:#DED6C5;
}

.1 {
float:left;
height:130px;
width:303px;
display:block;
background-image:url('images/organizational.jpg');
}

I modified the names in this post to "1,2,3,1b,2b,3b" to clean it up and be easier to understand. 我将这篇文章中的名字修改为“1,2,3,1b,2b,3b”来清理它并且更容易理解。

The CSS for the others is the same as 1 and 1b just with the names changed. 其他人的CSS与1和1b相同,只是名称已更改。

Your error is in your condition. 你的错误是你的错。

When using if , valid operator are || 使用if ,有效运算符为|| && == === < > <= >= but you are using = . && == === < > <= >=但使用的是=

Try using == and it should work. 尝试使用==它应该工作。

EDIT : 编辑:

Anthony Hessler added a good optimisation for your code, you should check it out! Anthony Hessler为您的代码添加了一个很好的优化,你应该检查出来!

In addition to the answer about the correct conditional formatting, I'd suggest updating your visibility toggle function to reduce the amount of repeated code, which makes it easier to add more dive, should the need arise. 除了关于正确条件格式的答案之外,我建议更新您的可见性切换功能以减少重复代码的数量,这样可以在需要时更容易添加更多潜水。 Something like this should work quite well for you, as it loops through the number of divs you have, and does 1 conditional check to see if the passed-in number matches the loop iteration value, and sets visibility accordingly. 像这样的东西对你来说应该很好用,因为它遍历你拥有的div的数量,并进行1次条件检查以查看传入的数字是否与循环迭代值匹配,并相应地设置可见性。

function toggleVisibility(divid) {
    var i;
    for (i = 1; i < 4; i += 1) {
        if (divid === i) {
            document.getElementById(i + "b").style.visibility = "visible";
        } else {
            document.getElementById(i + "b").style.visibility = "hidden";
        }
    }
}

Hope it helps! 希望能帮助到你!

It's possible to have a javascript-free solution if you can bear with moving the links above the divs (or with some clever positioning). 如果您可以使用移动div上方的链接(或者使用一些聪明的定位),则可以使用无javascript的解决方案。

Example: http://jsfiddle.net/XUwwY/ 示例: http//jsfiddle.net/XUwwY/

Concept: 概念:

.org:hover ~ #orginfo {
     visibility: visible;   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM