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

oxf4rvwz  于 2022-12-03  发布在  jQuery
关注(0)|答案(7)|浏览(141)

以下是将隐藏的所需范围

<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});
}
yhxst69z

yhxst69z1#

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!

8fq7wneg

8fq7wneg2#

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/

pjngdqdw

pjngdqdw3#

请尝试更改css的visible属性。

kmbjn2e3

kmbjn2e34#

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

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

mzaanser5#

使用下列程式码变更span的可见性:

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

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

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

46qrfjad6#

另一个道:

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

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

jQuery hide() =设置display: nonevisibility: hidden,带过渡
jQuery show() =设置display: blockvisibility: visible,带过渡

w9apscun

w9apscun7#

如果您使用的是旧版本的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);
}

相关问题