简体   繁体   English

JQuery的每个函数都可以用在隐藏的DOM对象上吗?

[英]Can JQuery each function be used on DOM objects that are hidden?

I am trying to find the total outer width of all "li" elements in a menu but my code don't seem to work so I am wondering is it because the parent UL element is set to display none? 我试图在菜单中找到所有“li”元素的总外部宽度,但我的代码似乎不起作用所以我想知道是因为父UL元素设置为不显示?

If so how to get around this? 如果是这样如何解决这个问题?

<ul>
<li>menu item 1</li>
<li>menu item 2<ul>
     <li>sub menu item 1</li>
     <li>sub menu item 2</li>
</ul></li>
</ul>

So obviously my sub menu item is set to display none until it is hovered over. 所以很明显我的子菜单项设置为显示无,直到它悬停在上面。

And my JS is like this 而我的JS就是这样的

var totalWidth = 0;
jQuery("ul ul li").each(function() {
     totalWidth += jQuery(this).outerWidth();
});
alert(totalWidth);

When I alert this, it is always zero... 当我提醒这一点时,总是零...

Anyway you can hack your way out of it...: 无论如何,你可以破解你的方式...:

var totalWidth = 0;
jQuery("ul ul").show();
jQuery("ul ul li").each(function() {
     totalWidth += jQuery(this).outerWidth();
});
jQuery("ul ul").hide();
alert(totalWidth);

Not sure you have hidden the ul ul selector but see the system? 你不确定你是否隐藏了ul ul选择器但是看到了系统?

Some CSS hacks maybe: 一些CSS黑客可能:

http://jsfiddle.net/ywtBH/ http://jsfiddle.net/ywtBH/

http://jsfiddle.net/ywtBH/2/ http://jsfiddle.net/ywtBH/2/

http://jsfiddle.net/ywtBH/5/ http://jsfiddle.net/ywtBH/5/

After chat: 聊天后:

The solution was to put white-space:none; 解决方案是放white-space:none; and... See: http://jsfiddle.net/dGhAp/2/ 和......见: http//jsfiddle.net/dGhAp/2/

With float:left; 随着float:left;

--- PARENT ---------------------
|------------------------       |
|   elem1   |   elem2   |       |
|           |           |       |
|           |           |       |
-------------------------
    elem3   |
            |
            |
-------------

With display:inline-block; display:inline-block;

--- PARENT ---------------------
|-------------------------------|---
|   elem1   |   elem2   |elem3  |   |
|           |           |       |   |
|           |           |       |   |
------------------------------------

The issue isn't the enumeration but the fact that outerWidth returns zero for all of the enumerated elements because they are not displayed. 问题不是枚举,而是外部宽度为所有枚举元素返回零的事实,因为它们不会显示。

If you can change the way you hide your UL element to use visibility: hidden instead of display: none then outerWidth should return an actual width, which would solve your problem. 如果您可以更改隐藏UL元素的方式以使用visibility: hidden而不是display: none那么outerWidth应返回实际宽度,这将解决您的问题。

What is this function for? 这个功能是什么用的? You coudl position the menu items off the screen using something like top: 999999px so the user doesn't see it but its hidden. 您可以使用top:999999px等菜单将菜单项放在屏幕上,这样用户就不会看到它而是隐藏它。 By doing that jquery can pick out the width. 通过这样做,jquery可以选择宽度。 Part of your hover event would be to set/remove the top style so that its positioned as expected. 悬停事件的一部分是设置/删除顶部样式,以使其按预期定位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM