CSS/Javascript 鼠标悬停弹出框

Kev*_*vin 5 html javascript css mouseover

我有一个带有 javascript/css 内容框的表格单元格,它会在鼠标悬停时弹出。

页面上有 20 个单元格。一切正常,当您将鼠标悬停在产品链接上时,您会看到内容框。但是,我想在用户可以选择的内容框中放置一个 LINK。因此,弹出框必须保持足够长的时间,以便用户将鼠标悬停在鼠标上以单击链接。

真的,我希望 OnMouseOver 保持打开状态,直到一两秒过去和/或用户 OnMouseOver 的另一个单元格。

我遇到的问题是弹出框没有保持打开状态(由于 OnMouseOut)来单击链接。如果我关闭 OnMouseOut(我尝试过),那么所有弹出框都会保持打开状态,所以这也不起作用。

我的 CSS 看起来像这样:

<style type="text/css" title="">
    .NameHighlights         {position:relative; }
    .NameHighlights div     {display: none;}
    .NameHighlightsHover    {position:relative;}
    .NameHighlightsHover div {display:block;position:absolute;width: 15em;top:1.3em;*top:20px;left:70px;z-index:1000;}
</style>
Run Code Online (Sandbox Code Playgroud)

和 html:

<td>
    <span class="NameHighlights" onMouseOver="javascript:this.className='NameHighlightsHover'" onMouseOut="javascript:this.className='NameHighlights'">
    <a href="product link is here">Product 1</a>
           <div>
            # of Votes:  123<br>
            % Liked<br>
            <a href="product review link>See User reviews</a>
            </div>
    </span>
</td>
Run Code Online (Sandbox Code Playgroud)

那么,如何让弹出框保持打开足够长的时间以单击链接,但如果另一个内容框被激活,它又会消失?

提前致谢。

dfs*_*fsq 4

您必须为此任务改进 HTML 标记,需要摆脱内联事件处理程序:

<span class="NameHighlights">
    <a href="product link is here">Product 1</a>
    <div>
        # of Votes:  123<br>
        % Liked<br>
        <a href="product review link">See User reviews</a>
    </div>
</span>
Run Code Online (Sandbox Code Playgroud)

然后你必须将事件绑定到所有.NameHighlights跨度:

var span = document.querySelectorAll('.NameHighlights');
for (var i = span.length; i--;) {
    (function () {
        var t;
        span[i].onmouseover = function () {
            hideAll();
            clearTimeout(t);
            this.className = 'NameHighlightsHover';
        };
        span[i].onmouseout = function () {
            var self = this;
            t = setTimeout(function () {
                self.className = 'NameHighlights';
            }, 300);
        };
    })();
}
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/3wyHJ/

所以想法就是使用setTimeout方法。

注意:我使用的querySelectorAll是IE7不支持的,如果你需要支持它,那么你可以使用该getElementsByClassName方法的任何实现。