2012-03-05 6 views
4

OK。私は何とかノブなんだけど、ノーブッシュじゃない。 :)jQuery複数のセレクタとドット表記?

ドット表記と同様の結果を持つjQueryでfind()を実行したいとか、 "& &"です。ここでは(動作しない)の例である:

data.find("continent_country[id = 'us'].state[id = 'in']").each(function(){ 
// what to do 
} 

または

data.find("continent_country[id = 'us'] && state[id = 'in']").each(function(){ 
// what to do 
} 

私はそうのように、カンマを試すように言われています

data.find("continent_country[id = 'us'], state[id = 'in']").each(function(){ 
// what to do 
} 

...しかし、ということ間違った項目を返します。

私のXMLは次のようになります。

<continent_country id="us" name="U.S."> 
     <state id="al" name="Alabama"> 
      <city url="" name="auburn"/> 
      <city url="" name="birmingham"/> 
      <city url="" name="dothan"/> 
      <city url="" name="florence/muscle shoals"/> 
      <city url="" name="gadsden-anniston"/> 
      <city url="" name="huntsville/decatur"/> 
      <city url="" name="mobile"/> 
      <city url="" name="montgomery"/> 
      <city url="" name="tuscaloosa"/> 
     </state> 
     <state>//more states</states> 
    </continent_country> 
    </continent_country id="eu" name="Europe"> 
     <state>//more states</states> 
    </continent_country> 

一部の州/地方/国は、私が指定したcontinent_countryの状態を検索したい理由は、同じIDを共有しています。

事前のおかげで...

+0

を選択してください。まず、あなたは私たちと一緒にすべての大陸の国を取得し、その選択から、あなたは再びすべてをフィルタリングしますか? –

+0

私は元々それをしましたが、 "ワンラインソリューション"が必要でした。子セレクタ ">"は完全に機能しました。 – TSNev

答えて

4

私はあなたが子供の表記をしたいと思う:

http://api.jquery.com/child-selector/

data.find('continent_country[id = 'us'] > state[id='in']).each(function(){ 
    //do your stuff here 
} 
+0

パーフェクト。ありがとうございます! – TSNev

+0

うれしい私は助けることができました! – Patricia

+0

wow 3 downvotes?人々に何が間違っていますか?これはまさにオペラが望んでいたものです! – Patricia

1

あなただけの、例えば、新しいセレクタに基づいて選択に多くの要素を追加add()方法を使用することができます

data.find("continent_country[id = 'us']").add("state[id = 'in']").each(function(){ 
    // what to do 
} 
+0

これはセレクタ問題なので必要ありません – soju

+1

しかし、この方法はまだ動作します。他の方法があるからといって、このアプローチが間違っているとは限りません。 – danwellman

+0

余分な方法であるにもかかわらず、セレクタがより簡単なので、余分なメソッド – soju

0

あなたが:hasセレクタ、.has機能を使用するか、または状態上記最も近いcontinent_countryを選択することができます。おそらく最も効率的なhas関数があります。

私は誤解してあなたが <state>要素を取得したい場合はスペースを使用し、...あなたは <continent_country>要素を取得したいと仮定しています
data.find("continent_country[id='us']").has("state[id='in']") 

data.find("continent_country[id='us'] state[id='in']") 
0

あなたが.has()方法を試してみてもらえますか?

data.find("continent_country[id = 'us']").has("state[id = 'in']").each(function(){ 
// what to do 
} 

これでうまくいくはずです。

-1

のようなもの、あなたが国内の状態にしたい場合は、descendant selectorcontinent_country[id='us'] state[id='in']

かÂを使用する必要がありますchild selectorcontinent_country[id='us'] > state[id='in']

詳細については、jquery selectors

+0

downvoter自分自身を説明する – soju

関連する問題