使用 jQuery(也许还有 CSS),如何隐藏元素而不折叠和动画?

发布于 2024-11-30 04:37:05 字数 725 浏览 3 评论 0原文

以下是将隐藏的所需范围

<span class="lastSave" name="lastSave">Last Saved by <i name="lastSavedBy"></i> at <i name="lastSaved"></i> </span>

。页面中的下面有一个表格。

以下是在某些事件中调用的 javascript 中的相关函数。 请注意,这会使桌子向上移动。

function hideLastSaved(form){
    jQuery(' [name="lastSave"]').hide();
}

function showLastSaved(form){
    jQuery(' [name="lastSave"]').show();
}

为了替换 javascript 中的上述片段,我尝试了以下操作。这不会使桌子向上移动。但这个有某种淡出,这在我的应用程序中是不希望的。

function hideLastSaved(form){
    jQuery(' [name="lastSave"]').animate({opacity:0});
}

function showLastSaved(form){
    jQuery(' [name="lastSave"]').animate({opacity:100});
}

The following is the desired span which will be hidden

<span class="lastSave" name="lastSave">Last Saved by <i name="lastSavedBy"></i> at <i name="lastSaved"></i> </span>

There is a table below this in the page.

The following is the relevant functions in the javascript invoked at some event.
Note that this one makes the table move up.

function hideLastSaved(form){
    jQuery(' [name="lastSave"]').hide();
}

function showLastSaved(form){
    jQuery(' [name="lastSave"]').show();
}

In replace of the above snippets in the javascript, I tried the following. This does not make the table move up. But this one has some kind of fading out which is not desired in my application.

function hideLastSaved(form){
    jQuery(' [name="lastSave"]').animate({opacity:0});
}

function showLastSaved(form){
    jQuery(' [name="lastSave"]').animate({opacity:100});
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

两个我 2024-12-07 04:37:05

好吧,我希望其他一切都保持原样,所以 jQuery 中的 .hide() 不起作用,因为这相当于在 CSS 中添加以下类

.hide{
display: none
}

另外,我不想要任何形式的动画,因此 .animate() 并将不透明度从 0 更改为 1 不是我想要的。我已经尝试过了,我可以看到该元素淡出。我希望它立即消失,而页面中的所有其他内容都不会移动。

我决定使用 .css( propertyName, value ),

function hideLastSaved(form){
    jQuery(' [name="lastSave"]').css('visibility','hidden');
}

function showLastSaved(form){
    jQuery(' [name="lastSave"]').css('visibility','visible');
}

这是添加这样一个类的更短方法:

.hider{
visibility: hidden
}

添加到元素中

function hideLastSaved(form){
    $(' [name="lastSave"]').addClass('hider');
}

function showLastSaved(form){
    $(' [name="lastSave"]').removeClass('hider');
}

使用类似于 Jamie Dixon 建议的方式

。我还没有尝试过 Nightw0rk 的建议:

function hideLastSaved(){
    jQuery(' [name="lastSave"]').css('opacity','0');
}

function showLastSaved(){
    jQuery(' [name="lastSave"]').css('opacity','1');
}

但会尝试一下,这样我就知道它是什么样子的。

非常感谢大家提出的精彩建议!

Well I wanted everything else to stay in place, so the .hide() in jQuery didn't work because this is equivalent to adding the following class in CSS

.hide{
display: none
}

Also, I didn't want any form of animation so the .animate() and changing the opacity from 0 to 1 isn't what I am looking for. I have tried it and I can see the element fade out. I want it to disappear instantly without all the others in the page move.

I decided to use the .css( propertyName, value )

function hideLastSaved(form){
    jQuery(' [name="lastSave"]').css('visibility','hidden');
}

function showLastSaved(form){
    jQuery(' [name="lastSave"]').css('visibility','visible');
}

which is the shorter way of adding a class like this one:

.hider{
visibility: hidden
}

to the element using

function hideLastSaved(form){
    $(' [name="lastSave"]').addClass('hider');
}

function showLastSaved(form){
    $(' [name="lastSave"]').removeClass('hider');
}

similar to what Jamie Dixon suggests.

I have not tried what Nightw0rk suggests:

function hideLastSaved(){
    jQuery(' [name="lastSave"]').css('opacity','0');
}

function showLastSaved(){
    jQuery(' [name="lastSave"]').css('opacity','1');
}

but will try it just so I know how it looks like.

Thank you all very much for your brilliant suggestions!

双手揣兜 2024-12-07 04:37:05

hide 方法将元素的 display 属性更改为 none

如果您想在隐藏元素的同时保留元素的位置,请添加一个将 visibility 设置为 hidden 的类。

CSS:

.hider{
   visibility:hidden;
}

JS

function hideLastSaved(form){
    $(' [name="lastSave"]').addClass('hider');
}

function showLastSaved(form){
    $(' [name="lastSave"]').removeClass('hider');
}

这是一个非常基本的演示:http://jsfiddle .net/dWw7F/

The hide method changes the display property of the element to none.

If you want to keep the elements position whilst hidding it, add a class that sets visibility to hidden.

CSS:

.hider{
   visibility:hidden;
}

JS

function hideLastSaved(form){
    $(' [name="lastSave"]').addClass('hider');
}

function showLastSaved(form){
    $(' [name="lastSave"]').removeClass('hider');
}

Here's a very basic demo: http://jsfiddle.net/dWw7F/

夜吻♂芭芘 2024-12-07 04:37:05

尝试更改 css 可见属性。

Try changinging the css visible property.

海的爱人是光 2024-12-07 04:37:05
function hideLastSaved(){
    jQuery(' [name="lastSave"]').css('opacity','0');
}

function showLastSaved(){
    jQuery(' [name="lastSave"]').css('opacity','1');
}
function hideLastSaved(){
    jQuery(' [name="lastSave"]').css('opacity','0');
}

function showLastSaved(){
    jQuery(' [name="lastSave"]').css('opacity','1');
}
温柔一刀 2024-12-07 04:37:05

使用以下代码更改 span 的可见性:

$('#uniqueSpanId').animate({opacity: 0.0});

这会将 uniqueSpanId 的范围的不透明度更改为 0,使其不可见。将其更改回来很简单,执行相反的命令:

$('#uniqueSpanId').animate({opacity: 1.0});

Use the following code to change the visibility of the span:

$('#uniqueSpanId').animate({opacity: 0.0});

This will change the opacity of a span with uniqueSpanId to 0 making it invisible. Changing it back from that is simple doing the opposite command:

$('#uniqueSpanId').animate({opacity: 1.0});
春风十里 2024-12-07 04:37:05

另一种方法:

function hideLastSaved(form){
    jQuery(' [name="lastSave"]').css('display','none');
}

function showLastSaved(form){
    jQuery(' [name="lastSave"]').css('display','block');
}

jQuery hide() = 设置 display: nonevisibility: hide,并带有过渡

jQuery show() = 设置 display: blockvisibility:visible,并带有过渡

Another way:

function hideLastSaved(form){
    jQuery(' [name="lastSave"]').css('display','none');
}

function showLastSaved(form){
    jQuery(' [name="lastSave"]').css('display','block');
}

jQuery hide() = set display: none and visibility: hidden, with a transition

jQuery show() = set display: block and visibility: visible, with a transition

飘逸的'云 2024-12-07 04:37:05

如果您使用的是较旧版本的 JQuery,则使用以下内容:

function hideLastSaved(form){
jQuery(' [name="lastSave"]').fadeOut(1);
}

function showLastSaved(form){
jQuery(' [name="lastSave"]').fadeIn(1);
}

“1”只是设置 1 毫秒的时间刻度,这在技术上没有什么意义,如果您使用的是较新版本的 JQuery,则只需添加 $ 而不是 JQuery:

function hideLastSaved(form){
$('[name="lastSave"]').fadeOut(1);
}

function showLastSaved(form){
$('[name="lastSave"]').fadeIn(1);
}

If you are using the older versions of JQuery then use the following:

function hideLastSaved(form){
jQuery(' [name="lastSave"]').fadeOut(1);
}

function showLastSaved(form){
jQuery(' [name="lastSave"]').fadeIn(1);
}

The "1" simply sets a time scale of 1 millisecond which is technically nothing and if you are using the newer versions of JQuery then just add $ instead of JQuery:

function hideLastSaved(form){
$('[name="lastSave"]').fadeOut(1);
}

function showLastSaved(form){
$('[name="lastSave"]').fadeIn(1);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文