gpt4 book ai didi

Jquery简单隐藏Div但设置较小的高度

转载 作者:行者123 更新时间:2023-12-01 06:28:43 25 4
gpt4 key购买 nike

为什么这段代码会尝试隐藏 div 然后再次显示它?使用火狐浏览器

$(document).ready(function(){

$("#tool1").click(function(){
if($('#status').height() > 100){
$('#status').css('height','50px').show();
}
});

});

感谢您的帮助,到目前为止他们都成功了。但这是我的确切代码 DEMO

最佳答案

$("#tool1").click(function(){
if($('#status').height() > 100){
$('#status').hide(500, function() { // hide the div
$(this)
.css('height','50px') // change the height
.show(500); // then show again
});
}
});

<强> DEMO

根据上面的评论

$("#tool1").click(function(){
var orig = $('#status').height(),
target = orig == 250 ? 50 : 250;
$('#status').animate({
height: target
},1000);
});

<强> DEMO

根据下面的评论

  • 按钮位于#status

<强> Button inside demo

根据您的demo

更改代码如下:

$(document).ready(function() {
$("#tool1").click(function(e) {
e.preventDefault(); // preventDefault() for pare reload
var $statusDiv = $('#status');
if ($statusDiv.height() > 100) {
$statusDiv.height(50);
}
else {
$statusDiv.height(250);
}
});
});

<强> DEMO

注意

您还可以从 #tool1 中删除 href

目前#tool1是链接标签,如果你想使用按钮,只需从上面的代码中删除e.preventDefault()即可。

关于Jquery简单隐藏Div但设置较小的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11331996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com