【发布时间】:2012-10-26 05:42:27
【问题描述】:
我有一个要求,我必须根据 mouseover 和 mouseout 事件显示替代元素。
默认元素是一个超链接(它会一直显示,除了onmouseover),alternate元素是一个下拉列表元素(它应该在超链接的鼠标悬停时显示。
我正在使用 javascript 来切换两个元素的显示(隐藏/取消隐藏)。
问题是鼠标的范围仅限于选择元素而不是它的选项值,即当我尝试使用鼠标从 DDL 中选择选项值时,浏览器认为它是 mouseout 并切换元素。我想要即使我在下拉列表选项元素上运行鼠标时也不应该发生鼠标移出。有没有办法解决这个问题?
这是我到目前为止所写的。
JS代码
function showDDl()
{
document.getElementById("LINK").style.display='none';
document.getElementById("ddlLanguage").style.display='';
}
function hideDDl()
{
document.getElementById("ddlLanguage").style.display='none';
document.getElementById("LINK").style.display='';
}
HTML 代码
<!--CODE FOR Hyperlink.. this is to be displayed at all times except at mouseover -->
<div id="LINK">
<a href='#' onmouseover="showDDl()" >
(Change Language)
</a>
</div>
<!--CODE FOR SELECT ELEMENT dispalyed at mouseover -->
<select style="display: none;" id="ddlLanguage" name="ddlLanguage" onmouseout="hideDDl()">
<option value="en"><bean:message key="common.lang.english"/></option>
<option value="fr"><bean:message key="common.lang.francais"/></option>
<option value="de"><bean:message key="common.lang.deutsch"/></option>
<option value="it"><bean:message key="common.lang.italiano"/></option>
<option value="es"><bean:message key="common.lang.espanol"/></option>
<option value="nl"><bean:message key="common.lang.nederlands"/></option>
</select>
【问题讨论】:
-
如果您要操作 DOM 对象,为什么不使用 Jquery?你只需要 3 行 ;)
标签: javascript html html-select