使用jQuery显示和隐藏div

当您单击链接时,它会显示div“somecontent”。 如果再次点击链接,它将隐藏。 如何在hover在链接上时进行div显示并在移开时隐藏而不是在点击时显示和隐藏?

这是代码:

 $(document).ready(function(){ $(".showcontent").click(function(){ $(".somecontent").toggle("fast"); }); });  Show 

some content

只需替换/删除click事件并添加mouseover和mouseout(您可以链接原始元素选择)。

  Show 

some content

编辑:有科林的建议..

稍微重构元素,这应该提供您想要的结果。

 
Show
Close

some content
Link 1
Link 2
Link 3

只需用“鼠标hover”替换“点击”…

  Show 

some content

RE解决“hover在div上”的问题。 你可以设置这样的东西:

 var revealLink = $('.showcontent'); var revealDiv = $('.somecontent'); var revealTimeout = 0; // we attach the mouseover/out events to both. revealLink.add(revealDiv).hover(function() { clearTimeout(revealTimeout); revealDiv.show(); }, function() { // give user 500 ms to get over the other element to not hide. revealTimeout = setTimeout(function() { revealDiv.hide(); }, 500); }); 

鼠标输出处理程序设置超时,当您再次鼠标hover元素时,该超时将被清除。