简体   繁体   English

使用CSS或Javascript显示和隐藏DIV?

[英]Show and Hide a DIV with CSS or Javascript?

I just saw a demo that had this jquery code to show and hide a dive on hover, can't this be done with just regualr css though? 我刚刚看到一个演示了这个jquery代码来显示和隐藏悬停的潜水,这不能用regualr css完成吗? And if you can do it with css is there any advantage of doing it with javascript? 如果你能用css做到这一点,用javascript做这个有什么好处吗?

$('.comment').hover(function() {
  $(this).children('.delete').show();
}, function() {
  $(this).children('.delete').hide();
});

CSS hover works fine with anchor tags, but IE6 does not recognize hover events on things like li tags. CSS悬停适用于锚标签,但IE6无法识别li标签之类的悬停事件。

If you were using an anchor tag, however, you could achieve the same effect in CSS: 但是,如果您使用的是锚标记,则可以在CSS中实现相同的效果:

a.comment       .delete { display: none; }
a.comment:hover .delete { display: block; }

您可以使用CSS执行此操作,但IE6仅支持锚标记(A)上的:hover伪类,因此它不常见。

Jody is correct. 乔迪是对的。 Check out the docs for the CSS Display property. 查看CSS Display属性的文档。

There is more functionality that the .hover will do. .hover将提供更多功能。 If you provide it more than 2 functions it will cycle through all the functions. 如果您提供2个以上的功能,它将循环执行所有功能。 Example

$('.comment').hover(
    function(){$(this).children('.delete.first').show()},
    function(){$(this).children('.delete.first').hide()},
    function(){$(this).children('.delete.second').show()},
    function(){$(this).children('.delete.second').hide()}
    );

That would show one set of children the first time they hover, then hide, and the next time show a different set of children. 这将显示一组孩子第一次盘旋,然后隐藏,下一次显示一组不同的孩子。

The hover function also works over multiple elements, and only fires if the mouse has left all the elements (not just when it leaves one and moves to another) 悬停功能也适用于多个元素,只有在鼠标离开所有元素时才会触发(不只是当它离开一个元素并移动到另一个元素时)

I dynamically create something like this on the server side. 我在服务器端动态创建这样的东西。 I'm sure there is a more efficient/prettier way but this usually serves my needs. 我确信有一种更有效/更漂亮的方式,但这通常可以满足我的需求。 Basically hides all the divs and un-hides the one that needs to be shown (passed as arg in function from onClick event). 基本上隐藏所有div并取消隐藏需要显示的div(从onClick事件中作为arg传递)。

function toggleTab(id)
{
    document.getElementById('divEnrollment').style.display='none';    
    document.getElementById('divSearch').style.display='none';
    document.getElementById('divMeeting').style.display='none';
    document.getElementById('divBenefit').style.display='none';
    document.getElementById('div' + id).style.display='block';

    document.getElementById('spnEnrollment').style.color='blue';    
    document.getElementById('spnSearch').style.color='blue';
    document.getElementById('spnMeeting').style.color='blue';
    document.getElementById('spnBenefit').style.color='blue';
    document.getElementById('spn'+id).style.color = 'red';
}

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

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