English 中文(简体)
CSS: Hover in one DIV, how to show information from another DIV?
原标题:CSS: Hover in one DIV, how to display information from another DIV?

Got a list with 3 items. Upon hovering over a item information shall be displayed. Works fine within same <DIV>. How about displaying information from another <DIV>, I guess this should be possible, isn t it?

Initially only the three lisitings are displayed. When hovering over listing 2 the content of #special can be displayed, no problem.

When hovering over listing 3 the content of #AlsoSpecial is not displayed, Why?. Is there a remedy?

样本:

<div id="top">
   <ul>
        <li class="left">listing 1</li>
        <li class="center">listing 2</li>
        <li class="right">listing 3</li>
        <li><div id="special">
            <p>bla1bla1bla1</p>
        </div></li>
    </ul>
</div>

<div id="content">
    <div id="AlsoSpecial">
        <p>bla2bla2bla2</p>
    </div>
</div>

Sample CSS:

#top > li {display:inline;}

/*this works fine*/
div#special {display:none;}
.center:hover div#special {display:block;}

/*this does not work, WHY?*/
div#AlsoSpecial {display:none;}
.right:hover div#AlsoSpecial {display:block;}
最佳回答

When an element is sibling of another one, you should use the + selector. So, doing this should do it.-

li:hover + div#special

However, note that UL elements expect only LI elements inside, adding something different is illegal and you should not.

I recommend you to place the DIV element inside of the element you want to hover. So a much better solution would be

...
<li>text
    <div>contents</div>
</li>
...

......仿照CSS的甄选者。 -

li:hover > div#special

考虑使用}=“special”,而不是ID,如果你想对这一 h余事件作出不止一个DIV反应,......记住,身份证是独一无二的,不能用于一个以上要素。

如果你想与科索沃安全局一道做一个“不匹配”的影子,那么你就不得不使用Javascript,这是在这里的Javascript中这样做的一个例子。 -

var toShow  = document.getElementById( alsoSpecial ),
    toHover = document.getElementById( toHover ), // ...for instance.
    showOrNot = function(e)
    {
        toShow.style.display = (e.type ==  mouseover )?
             block :  none ;
    };
toHover.addEventListener( mouseover , showOrNot, 1);
toHover.addEventListener( mouseout ,  showOrNot, 1);

我把该法典打成一类,没有尝试。 如果它不工作,我希望它能干 works,请让我知道。

页: 1

问题回答

所提供的两个答复都是正确的,但我决定采用以下尽可能少的法典(可能是过时的):

<script type="text/javascript">
/*<![CDATA[*/
    function showDetails()
    {document.getElementById("AlsoSpecial").style.display="block";}
    function hideDetails()
    {document.getElementById("AlsoSpecial").style.display="none";}
/* ]]> */
</script>

<body>
    ...
    <a title="Display Details" href="#AlsoSpecial" rel="nofollow" onmouseover="showDetails()" onmouseout="hideDetails()">View Details</a>
    ...
    <div id="AlsoSpecial">
        <p>bla2bla2bla2</p>
    </div>
</body>

谢谢大家。





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!