gpt4 book ai didi

jquery - 悬停后是否可以在不启动悬停本身的情况下获取 div 的属性?

转载 作者:太空宇宙 更新时间:2023-11-04 07:52:57 25 4
gpt4 key购买 nike

悬停后是否可以在不启动悬停本身的情况下获取内容未知的 div 的高度?

jquery 能帮上忙吗?

.content-container{
width:20%;
border:1px solid #000;
}

.content-container:hover{
width:100%;
}
<div class="content-container">Some content that can be anything of any length</div>

最佳答案

你必须做一些事情来应用规则,进行测量,然后删除那个东西。一种简单的方法是更新规则以也适用于类,然后添加类、测量、删除类:

var container = $(".content-container");
var unexpanded = container.height();
console.log("height when unexpanded: " + unexpanded);
container.addClass("expand");
var expanded = container.height();
container.removeClass("expand");
console.log("height when expanded: " + expanded);
.content-container{
width:20%;
border:1px solid #000;
}

.content-container:hover, .content-container.expand {
width:100%;
}
<div class="content-container">Some content that can be anything of any length</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

测量第一个/唯一的.content-container。如果有多个,则需要一个 each 循环:

$(".content-container").each(function() {
var container = $(this);
// ...
});

关于jquery - 悬停后是否可以在不启动悬停本身的情况下获取 div 的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47593108/

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