开发者

Select an element with jQuery selector regardless of ID

I have the following code:

<div>
 <div id="someID"><h1>Blah</h1></div>
 <div id="someID2"><h1>Blah</h1></div>
</div>

and I am trying to select in jQuery (for the accordion in jQuery UI). However, the following does not work unless I remove the ID from the div.

$(开发者_StackOverflow中文版'> div > h1')

Is there some way to tell the selector to ignore the ID, or am I going about this the wrong way?

Edit: The actual use I'm going for is in the sortable accordion in jQuery UI, with IDs on the div elements. An example of what I'm doing, with source, can be found here:

http://jqueryui.com/demos/accordion/#sortable


Edit: As I have a feeling this may be related to jQueryUI, rather than the selectors themselves, below is the code I'm actually trying to run.

http://jsfiddle.net/Zgkj4/1/

Working example without ID in div:

http://jsfiddle.net/Zgkj4/2/


You could try selecting the <h1> with a parent <div> that has an id attribute

$("div > div[id] > h1")

working example: http://jsfiddle.net/YjSvU/


ignores div w/o id: http://jsfiddle.net/YjSvU/1/


Yes, your selector should be a string, like this:

$('div > h1')


Your selector needs to be a string, so wrap it in single (') or double quotes (").

Also > div does not make sense, as > is direct descendant / child selector, and you have no parent on the left hand side (or otherwise context). Remove the leading >.

See it not working on jsFiddle.


http://codylindley.com/jqueryselectors/. A list of all jquery selectors. it should help you allot.


Use quotes, and im not sure about the first >

This should work:

$("div > h1")

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜