Javascript jQuery:获取父高度并指定给其中的选择器
我正在尝试通过jQuery将选择器的高度更改为其父级的高度,但尚未找到任何解决方案,您可以在本页中清楚地看到,我需要获得每个Javascript jQuery:获取父高度并指定给其中的选择器,javascript,jquery,Javascript,Jquery,我正在尝试通过jQuery将选择器的高度更改为其父级的高度,但尚未找到任何解决方案,您可以在本页中清楚地看到,我需要获得每个#accordio ul li的高度,并将其设置为。状态为绿色 谢谢大家! 您可以在这里看到工作示例: 第二个选项是迭代。句子divs: $("#accordion .sentence").each(function(index, value) { var $value = $(this); var current = $value.height() +
#accordio ul li的高度
,并将其设置为。状态为绿色
谢谢大家! 您可以在这里看到工作示例: 第二个选项是迭代。句子divs:
$("#accordion .sentence").each(function(index, value) {
var $value = $(this);
var current = $value.height() + 10;
$value.siblings(".status-green").height(current);
});
如果要使
.status变为绿色
s高度与关联的句子
高度相同,则必须计算它们的填充顶部
和填充底部
。以下是完整的代码:
而js将是:
$("#accordion .sentence").each(function () {
var current = $(this).height();
var p_top = $(this).css("padding-top");
var p_bot = $(this).css("padding-bottom");
$(this).parent().find(".status-green").css({
height:current,
"padding-top":p_top,
"padding-bottom":p_bot
});
});
试试这把小提琴。我刚刚修改了它,将其包装在document.ready函数中。谢谢,在我的页面上比较了您的示例和我的示例之后,我才意识到我的主要问题是什么?我在我的页面上使用Display:None作为#accordion,我删除了它,并用jQuery而不是样式表来设置它,这样jQuery就不会把它当作禁用的选择器,而且它工作得很好!再次感谢。感谢您的回答Javad,但是jQuery
.height()
也会计算填充。是的,您是对的,但并非在所有浏览器中都是如此!你可以试试看!这是基于我的经验,但你可以自己测试你可以注意mikhailov代码片段中的这一行:var current=$value.height()+10
指的是.height()
和填充
真的吗
$("#accordion .sentence").each(function () {
var current = $(this).height();
var p_top = $(this).css("padding-top");
var p_bot = $(this).css("padding-bottom");
$(this).parent().find(".status-green").css({
height:current,
"padding-top":p_top,
"padding-bottom":p_bot
});
});