如何在按钮单击时显示由 css 隐藏的 div
我对 Jquery 还很陌生,我想要一个隐藏的 div css: display:none
并且一旦单击按钮 $(..).slideDown
将执行,div 将显示其内容。我尝试过这个。
<style> .hidden-div{ display:none } </style>
<div class="hidden-div"> You can't see me </div>
<button onclick="show-div()"> Show Above Div </button>
<script>
function show-div(){
$('.hidden-div').slideDown('fast');
// more stuff...
}
</script>
这并没有真正将其正确地滑下来,因为它与其他所有东西重叠?另外,我尝试将 class="hidden-div"
设置为 class="display-div"
但无法执行幻灯片动画。
现在我可以在 div 之后说 $('hidden-div').hide()
并完全删除 css 但它会产生这个问题,我看到 div 然后它被隐藏,这是唯一的页面加载开始时需要 0.5 秒,但看起来很糟糕。
那么有人知道解决方案吗?
找到了解决我自己问题的方法..
//rehide the div, not sure why, but it works.
$('.hidden-div').hide();
//change class so it no longer display:none
//but it will not show the div as .hide() was execute above.
$('.hidden-div').attr('class','showing-div');
//Slide it down and everything works.
$('.hidden-div').slideDown('fast');
//this can be done in 1 liner. (thank you, Mohsen for chaining explanation)
$('.hidden-div').hide().attr('class','showing-div').slideDown('fast');
希望这对其他人有帮助。
I'm pretty new to Jquery and i want to have a hidden div css: display:none
and once a button has been clicked the $(..).slideDown
will execute and the div will show its conents. I try'd this.
<style> .hidden-div{ display:none } </style>
<div class="hidden-div"> You can't see me </div>
<button onclick="show-div()"> Show Above Div </button>
<script>
function show-div(){
$('.hidden-div').slideDown('fast');
// more stuff...
}
</script>
This dont really slide it down properly as it overlaps everything else ? Also i try'd just setting the class="hidden-div"
to class="display-div"
but then the slideDown animation cannot be executed.
Now i could say $('hidden-div').hide()
just after the div and remove the css altogether but it creates this problem where i see the div and then it gets hidden, its only 0.5sec thing at the start of the page load but its look bad.
So anyone know a solution?
Found a solution to my own problem..
//rehide the div, not sure why, but it works.
$('.hidden-div').hide();
//change class so it no longer display:none
//but it will not show the div as .hide() was execute above.
$('.hidden-div').attr('class','showing-div');
//Slide it down and everything works.
$('.hidden-div').slideDown('fast');
//this can be done in 1 liner. (thank you, Mohsen for chaining explanation)
$('.hidden-div').hide().attr('class','showing-div').slideDown('fast');
Hope this is helpful for someone else.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最好使用标准 javascript 并用 id 指向该 div
it's better to use standard javascript and point to that div with id
尝试使用 <和>而不是 [ 和 ],并正确格式化所有内容?
Try using < and > instead of [ and ], and format everything properly?
在您的 javascript 中,首先使用 jQuery 函数removeClass() 删除类“hidden-div”。
看看是否有帮助...
In your javascript, first remove the class 'hidden-div' using jQuery function removeClass().
See if that helps...
SlideDown
方法不是用于删除元素。您可以使用隐藏
或fadeOut
position:absolute
或为元素的父元素指定一定的height
。要同时具有向下滑动和淡入淡出效果,并使用非内联代码绑定事件,您可以使用以下代码:
也从
hidden-div
类中删除disply:none
。SlideDown
method is not for removing an element. You can usehide
orfadeOut
position:absolute
or give a certainheight
to element's parent.To have both slide down and fade effects and also binding the event using non-inline code you can use this code:
Remove that
disply:none
from thehidden-div
class too.