【问题标题】:jQuery multiple selectors with dot notation?带有点符号的jQuery多个选择器?
【发布时间】:2012-03-05 15:30:17
【问题描述】:

好的。我有点像菜鸟……但不是那种菜鸟。 :)

我希望在 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>

一些州/省/国家共享相同的id,这就是为什么我想在指定的continent_country 中找到一个州。

提前谢谢...

【问题讨论】:

  • 你有没有想过分开选择?就像首先你得到了我们所有的大陆国家,然后从那个选择中你再次过滤掉所有的东西?
  • 我最初是这样做的,但需要“单线解决方案”。子选择器“>”工作得很好。

标签: jquery xml jquery-selectors


【解决方案1】:

我认为你想要子符号:

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

类似

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

【讨论】:

  • 哇 3 票否决?那些人怎么了?这正是 op 想要的!
【解决方案2】:

您可以只使用add() 方法,该方法基于新的选择器将更多元素添加到选择中,例如

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

【讨论】:

  • 但是这种方式仍然有效。仅仅因为还有其他方式,并不代表这种方式是错误的
  • 这不是最好的方法,因为你调用了额外的方法
  • 秋千和环岛真的是因为虽然它是一种额外的方法,但选择器更简单。我们在这里谈论毫秒的差异。 是否有足够的洞察力来区分......?
  • 我什么时候谈到了性能?不管怎样,asker 想要“在指定的continent_country 中找到一个状态”,add 方法不是答案。
  • 您说我的答案更糟,因为这是一种额外的方法,除了性能方面,这意味着什么?使用额外的方法怎么会更糟?仅仅因为您没有提到“性能”这个词,并不意味着它没有被暗示。而且我驳斥了您的说法, add() 方法是许多可行的答案之一,它与您的答案不同
【解决方案3】:

您可以使用:has 选择器、.has 函数,或者选择状态上方最接近的continent_country。这是has 函数,这可能是最有效的。

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

假设您想要获取 &lt;continent_country&gt; 元素...如果我误解了并且您想要获取 &lt;state&gt; 元素,请使用空格:

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

【讨论】:

    【解决方案4】:

    你能试试.has()方法吗?

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

    应该可以的。

    【讨论】:

      【解决方案5】:

      如果您想要一个国家/地区内的州,您应该使用descendant selector continent_country[id='us'] state[id='in']

      或者child selectorcontinent_country[id='us'] &gt; state[id='in']

      你应该了解更多关于jquery selectors的信息

      【讨论】:

        猜你喜欢
        • 2021-12-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-30
        • 2016-02-26
        • 2013-07-04
        • 1970-01-01
        • 2013-07-08
        相关资源
        最近更新 更多