开发者

jquery selector find element from other

开发者 https://www.devze.com 2023-04-07 13:05 出处:网络
I have the follow html: <div id=\"x\"> <div id=\"x1\"> </div> .... </div> .... <div id=\"x2\">

I have the follow html:

<div id="x">

   <div id="x1">
   </div> 
   ....
</div>
....
<div id="x2">
   <table id="y">
   </table>
</div>

开发者_开发问答From "x" I need to reach "y", something like $("#x").find("#y")

Suppose that I do not know what has in the "...".

How to do this?


You can go up one level with parent() and use find() from there.

$('#x').parent().find('#y');


Try the following

$('#x').siblings().find('#y')

In truth though this makes little sense to do given that both elements in this case have id values. It's much faster to just search for #y directly. If you do actually have multiple ids with the same value that's a problem and you should move to an id generation scheme or classes


I think what you are looking for is...

$('#y', $('#x'))

The second parameter is the scope of the selector.

0

精彩评论

暂无评论...
验证码 换一张
取 消