jQuery:我应该在隐藏元素之前检查可见性吗? [英] jQuery : should i check for visibility before hiding an element?

查看:62
本文介绍了jQuery:我应该在隐藏元素之前检查可见性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在隐藏元素之前检查元素是否可见是最佳做法,还是只是隐藏它?有什么好处?

Is it best practice to check if an element is visible before hiding it, or is ok just to hide it? What are the benefits either way?

在下面的例子中,第一个检查隐藏 .foo 之前的可见性元素......

In the example below, the first checks for visibility before hiding the .foo element...

$('.dropdown').hover(function(){
    $(this).find('.menu').fadeToggle();
    if($('.foo').is(':visible')){
        $('.foo').fadeOut();
    };
});

$('.dropdown').hover(function(){
    $(this).find('.menu').fadeToggle();
    $('.foo').fadeOut();
});


推荐答案

这是来自 jQuery的.fadeOut()规范


注意:为避免不必要的DOM操作,.fadeOut()不会隐藏已被视为隐藏的元素。

Note: To avoid unnecessary DOM manipulation, .fadeOut() will not hide an element that is already considered hidden.

所以,放松......隐藏已隐藏的元素没有错。 JQuery会为你检查它。

So, relax... There's nothing wrong with hiding an element that's already hidden. JQuery will check it for you.

这篇关于jQuery:我应该在隐藏元素之前检查可见性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆