我如何使用jquery获取动态创建的没有ID的子元素的高度 [英] How do I use jquery to get the height of a dynamically created child element with no Id

查看:189
本文介绍了我如何使用jquery获取动态创建的没有ID的子元素的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个div,它动态获取两个图像始终加载并可能在一个div之间。图像或div都没有id与它们相关联(我不能让它们拥有Id)。用firebug检查它们,它们只是显示为< IMG> < DIV> 。我希望我能做到这样的事情...

  $(#parentDiv> DIV)。height(); 

或此...

  $(#parentDiv> DIV)[0] .height(); 

由于jquery $返回一个数组。第二个给javascript错误,所以我知道我在那里。我认为这些应该是接近的。有任何想法吗?

编辑:
这是我运行的html。

 < DIV id =parentDivname =parentDiv> 
< IMG style =DISPLAY:block; VERTICAL-ALIGN:bottom; CURSOR:pointerheight =17src =.../>

<! - 这个div可能会也可能不会在这里 - >
< DIV style =DISPLAY:block; BACKGROUND-IMAGE:url(...); WIDTH:16px; CURSOR:pointer; BACKGROUND-REPEAT:repeat-y; POSITION:relative; HEIGHT:144px; outline :none>
< DIV style =LEFT:0px; OVERFLOW:hidden; WIDTH:16px; POSITION:absolute; TOP:128px; HEIGHT:8px>
< IMG style =LEFT:0px; POSITION:absolute; TOP:0pxheight =8src =.../>
< / DIV>
< / DIV>

< IMG style =DISPLAY:block; VERTICAL-ALIGN:bottom; CURSOR:pointerheight =17src =.../>
< / DIV>


解决方案

要获得索引jQuery元素,请使用 eq()函数:

  $(#parentDiv> DIV) .EQ(0).height(); 

  $($(#parentDiv> DIV)[0])。height(); 

  $(#parentDiv> DIV:eq(0))。height(); 


I have a div that dynamically get's loaded with two images always and possibly one div in between. Neither the images or the div have id's associated with them (and I can't make them have Id's). Inspecting them with firebug they are just shown as <IMG> and <DIV>. I need to get the height of this child div when it exists.

I was hoping I could do something like this...

$("#parentDiv > DIV").height();

or this...

$("#parentDiv > DIV")[0].height();

Since jquery $ returns an array. The second one gives javascript errors so I know I'm off there. I think these should be close though. Any ideas?

Edit: Here is the html I am running against.

<DIV id="parentDiv" name="parentDiv">
    <IMG style="DISPLAY: block; VERTICAL-ALIGN: bottom; CURSOR: pointer" height="17" src="..." />

    <!-- this div may or may not be here -->
    <DIV style="DISPLAY: block; BACKGROUND-IMAGE: url(...); WIDTH: 16px; CURSOR: pointer; BACKGROUND-REPEAT: repeat-y; POSITION: relative; HEIGHT: 144px; outline: none">
        <DIV style="LEFT: 0px; OVERFLOW: hidden; WIDTH: 16px; POSITION: absolute; TOP: 128px; HEIGHT: 8px">
             <IMG style="LEFT: 0px; POSITION: absolute; TOP: 0px" height="8" src="..." />
        </DIV>
    </DIV>

    <IMG style="DISPLAY: block; VERTICAL-ALIGN: bottom; CURSOR: pointer" height="17" src="..." />
</DIV>

解决方案

to get an indexed jQuery element, use the eq() function:

$("#parentDiv > DIV").eq(0).height();

or

$($("#parentDiv > DIV")[0]).height();

or

$("#parentDiv > DIV:eq(0)").height();

这篇关于我如何使用jquery获取动态创建的没有ID的子元素的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆