jQuery:基于不同div的div高度

发布于 2025-01-01 18:24:58 字数 273 浏览 0 评论 0原文

我试图根据 jQuery 中父元素的高度来确定元素的高度,但收效甚微。我的代码如下:

$(document).ready(function() {
    $('#nav li:hover ul li ul').height() = $('#nav li ul').height() - 5;
    $('#nav li ul li ul').height() = $('#nav li ul').height() - 5;
});

有什么想法为什么这行不通吗?

I'm trying to make the height of an element based on the height of a parent element in jQuery with little success. My code is as follows:

$(document).ready(function() {
    $('#nav li:hover ul li ul').height() = $('#nav li ul').height() - 5;
    $('#nav li ul li ul').height() = $('#nav li ul').height() - 5;
});

Any ideas why this won't work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

云胡 2025-01-08 18:24:58

.height 是一个函数,因此当您调用 .height() 时,您无法为其赋值。在 jQuery 中,您可以通过将新高度作为参数传递给 .height 函数来设置元素的高度。

 $(document).ready(function() {
        $('#nav li:hover ul li ul').height($('#nav li ul').height() - 5);
        $('#nav li ul li ul').height($('#nav li ul').height() - 5);
    });

.height is a function, so when you call .height(), you can't assign a value to it. In jQuery you can set an element's height by passing in the new height as an argument to the .height function.

 $(document).ready(function() {
        $('#nav li:hover ul li ul').height($('#nav li ul').height() - 5);
        $('#nav li ul li ul').height($('#nav li ul').height() - 5);
    });
孤寂小茶 2025-01-08 18:24:58

你用错了...

$('#element').height( $('#otherelement').height() );

You're using it wrong...

$('#element').height( $('#otherelement').height() );
一桥轻雨一伞开 2025-01-08 18:24:58

如果要设置元素的高度,请将高度作为第一个参数传递给 jQuery 对象的 height 方法,如下所示。当您不向 height 方法传递任何参数时,它会返回匹配元素集中第一个元素的高度。

$(document).ready(function() {
    $('#nav li:hover ul li ul').height($('#nav li ul').height() - 5);
    $('#nav li ul li ul').height($('#nav li ul').height() - 5);
});

If you want to set the height of an element then pass the height as first argument to height method of jQuery object as shown below. When you don't pass any argument to height method it returns the height of the first element in the matched set of elements.

$(document).ready(function() {
    $('#nav li:hover ul li ul').height($('#nav li ul').height() - 5);
    $('#nav li ul li ul').height($('#nav li ul').height() - 5);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文