jQuery-隐藏元素的最佳方法? (以防止元素在实际隐藏之前闪烁) [英] jQuery - Best way to hide element? ( to prevent the element from flashing before actually hiding it )

查看:154
本文介绍了jQuery-隐藏元素的最佳方法? (以防止元素在实际隐藏之前闪烁)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我记得Opera ( Mostlikely it was Safari instead. )在某个时候有一个问题,如果您在元素上使用.hide(),它会在实际隐藏该元素之前短暂闪烁.

现在,如果您不想忽略由于某种原因而在其浏览器中未启用js的用户,则无法真正使用CSS在该元素中设置display: none;使其隐藏,然后使用js使其淡入. /p>

我最近注意到在Opera中不再发生这种情况.因此,我想知道在某些浏览器中是否仍然会发生这种情况,以防ive错过了这一点.哪种方法最安全? (在这种情况下,当然会忽略css方法.)

js .hide()

js .addClass('hide') css .hide { display: none; }

还有别的吗?

js element.style.display = "none"

js $(element).css({display:"none"})

Edit2:该问题实际上可能已经在Safari中出现.我还认为新的jquery版本可能已解决了该问题..我认为jquery网站上有一些有关此问题的错误报告,但我找不到这些错误报告..或者它仍然可能是较新的浏览器版本已解决了该问题. ..不确定.


Edit3:

因此,当我开始在Safari中而不是Opera中搜索此错误时,确实确实发现了很多有关此的信息.尽管我不能肯定地说它也从未在歌剧中发生过...

似乎不再存在此问题,并且使用.hide()是安全的,但是我了解到,当问题仍然存在时,此$(element).css({display:"none"})确实可以解决问题.

解决方案

您应该始终依靠使用jQuery hide()方法来隐藏任何元素,因为它可以处理所有浏览器.我在Opera中还没有看到任何问题.

出于相同的原因,即使显示任何元素,您也应该始终再次使用show()方法.

例如若要显示tr元素(如果我们说tr.css("display", "block")),则该元素在Firefox中将不起作用,因为它是一个表行,需要将其指定为.css("display", "table-row").但是,如果您使用tr.show(),则不必担心任何浏览器.

I remember that at some point Opera ( Mostlikely it was Safari instead. ) had a problem that if you used .hide() on element, it would flash briefly before it would actually hide the element.

Now if you dont want to ignore those who for some reason dont have js on in their browser you cant really use CSS to set display: none; in that element to hide it and then use js to for example fade it in.

I recently noticed that this didnt happen anymore in Opera. So, i'd like to know if this still does happen in some browsers incase ive missed that.. And asuming that this would happen. What way would be the safest way to do it? ( of course ignoring the css method in this case. )

js .hide()

js .addClass('hide') css .hide { display: none; }

Or something else?

Edit:

js element.style.display = "none"

js $(element).css({display:"none"})

Edit2: This issue might have been in Safari actually. I also got thinking that newer jquery versions might have fixed the issue.. As i think there was a few bug reports about this problem on jquery website but i couldnt find those bug reports ..Or it could still be that newer browser version fixed it.. Not sure.


Edit3:

So, I did indeed find a lot more about this when I started searching for this bug in Safari rather than Opera. Though i cant say for sure that it never happened in opera as well...

It would seem like this problem does not exist anymore and that it is safe to use .hide() but what I learned was that this $(element).css({display:"none"}) did fix the problem when the problem was still out there.

解决方案

You should always rely on using jQuery hide() method to hide any element because it takes care of all the browsers. I haven't seen any issues in Opera though.

Even to show any element you should always rely on using show() method again for the same reason.

E.g. To show a tr element if we say tr.css("display", "block") it will not work in Firefox because it is a table row and it needs to be specified as .css("display", "table-row"). But if you use tr.show() you dont have to worry about any browser.

这篇关于jQuery-隐藏元素的最佳方法? (以防止元素在实际隐藏之前闪烁)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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