如何在按钮单击时显示由 css 隐藏的 div

发布于 2024-12-06 09:22:33 字数 1341 浏览 1 评论 0原文

我对 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 技术交流群。

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

发布评论

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

评论(4

倾听心声的旋律 2024-12-13 09:22:33

最好使用标准 javascript 并用 id 指向该 div

<style> .hidden-div{ display:none } </style>

<div class="hidden-div" id="hidden-div"> You can't see me </div>

<button onclick="getElementById('hidden-div').style.display = 'block'"> Show Above Div </button>

it's better to use standard javascript and point to that div with id

<style> .hidden-div{ display:none } </style>

<div class="hidden-div" id="hidden-div"> You can't see me </div>

<button onclick="getElementById('hidden-div').style.display = 'block'"> Show Above Div </button>
口干舌燥 2024-12-13 09:22:33
$('.hidden-div').show('fast');

尝试使用 <和>而不是 [ 和 ],并正确格式化所有内容?

$('.hidden-div').show('fast');

Try using < and > instead of [ and ], and format everything properly?

郁金香雨 2024-12-13 09:22:33

在您的 javascript 中,首先使用 jQuery 函数removeClass() 删除类“hidden-div”。
看看是否有帮助...

In your javascript, first remove the class 'hidden-div' using jQuery function removeClass().
See if that helps...

似梦非梦 2024-12-13 09:22:33
  1. 通过 jQuery 绑定事件,不要使用内联 JavaScript 方法
  2. jQuery SlideDown 方法不是用于删除元素。您可以使用 隐藏fadeOut
  3. 顺便说一句,如果你想有一个向下滑动的效果,然后隐藏元素,你可以链接两个方法。
  4. 为了防止重叠,您需要使用 CSS。您可以使用 position:absolute 或为元素的父元素指定一定的height

要同时具​​有向下滑动和淡入淡出效果,并使用非内联代码绑定事件,您可以使用以下代码:

$('button').bind('click', function(){
$('.hidden-div').slideDown(500).fadeOut(1000); //500ms slide down and 1s fade out
})

也从 hidden-div 类中删除 disply:none

  1. bind events via jQuery, don't use inline JavaScript method
  2. jQuery SlideDown method is not for removing an element. You can use hide or fadeOut
  3. BTW if you want to have an slide down effect and then hiding the element you can chain two methods.
  4. To prevent overlaping you need to use CSS. You can use position:absolute or give a certain height 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:

$('button').bind('click', function(){
$('.hidden-div').slideDown(500).fadeOut(1000); //500ms slide down and 1s fade out
})

Remove that disply:none from the hidden-div class too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文