使用 jQuery(也许还有 CSS),如何隐藏元素而不折叠和动画?
以下是将隐藏的所需范围
<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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
好吧,我希望其他一切都保持原样,所以 jQuery 中的
.hide()
不起作用,因为这相当于在 CSS 中添加以下类另外,我不想要任何形式的动画,因此
.animate()
并将不透明度从 0 更改为 1 不是我想要的。我已经尝试过了,我可以看到该元素淡出。我希望它立即消失,而页面中的所有其他内容都不会移动。我决定使用 .css( propertyName, value ),
这是添加这样一个类的更短方法:
添加到元素中
使用类似于 Jamie Dixon 建议的方式
。我还没有尝试过 Nightw0rk 的建议:
但会尝试一下,这样我就知道它是什么样子的。
非常感谢大家提出的精彩建议!
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 CSSAlso, 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 )
which is the shorter way of adding a class like this one:
to the element using
similar to what Jamie Dixon suggests.
I have not tried what Nightw0rk suggests:
but will try it just so I know how it looks like.
Thank you all very much for your brilliant suggestions!
hide 方法将元素的
display
属性更改为none
。如果您想在隐藏元素的同时保留元素的位置,请添加一个将
visibility
设置为hidden
的类。CSS:
JS
这是一个非常基本的演示:http://jsfiddle .net/dWw7F/
The hide method changes the
display
property of the element tonone
.If you want to keep the elements position whilst hidding it, add a class that sets
visibility
tohidden
.CSS:
JS
Here's a very basic demo: http://jsfiddle.net/dWw7F/
尝试更改 css 可见属性。
Try changinging the css visible property.
使用以下代码更改
span
的可见性:这会将
uniqueSpanId
的范围的不透明度更改为 0,使其不可见。将其更改回来很简单,执行相反的命令:Use the following code to change the visibility of the
span
: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:另一种方法:
jQuery hide()
= 设置display: none
和visibility: hide
,并带有过渡jQuery show() = 设置
display: block
和visibility:visible
,并带有过渡Another way:
jQuery hide()
= setdisplay: none
andvisibility: hidden
, with a transitionjQuery show()
= setdisplay: block
andvisibility: visible
, with a transition如果您使用的是较旧版本的 JQuery,则使用以下内容:
“1”只是设置 1 毫秒的时间刻度,这在技术上没有什么意义,如果您使用的是较新版本的 JQuery,则只需添加 $ 而不是 JQuery:
If you are using the older versions of JQuery then use the following:
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: