【问题标题】:How can I find an element within parent element ID using jQuery?如何使用 jQuery 在父元素 ID 中查找元素?
【发布时间】:2012-12-08 00:08:08
【问题描述】:

我正在尝试在另一个 DIV id 中查找 DIV。

<div id="container1">
    <div class="inside">
    </div>
</div>

<div id="container2">
    <div class="inside">
    </div>
</div>

请注意,有 2 个具有“内部”类的 DIV。我正在尝试在特定容器中选择一个。

当只有一组容器时,这有效:

$carousel_container = $(carousel).find(".inside"); 

但是,当我定义父ID,然后尝试选择里面的div时,它不起作用:

$carousel_container = $(carousel).find("#" + o.theid + " .inside"); // where o.theid = container1 or container2

【问题讨论】:

  • 我怀疑o.theid 的值是否正确。
  • 确实如此。我可以触发一个警报框,它确实有正确的值。
  • @SheikhHeera:他没有
  • @JohnRobinson:carousel 是什么? -> jsfiddle.net/4XPsL
  • 如果carousel 不是变量那么它应该是$('#carousel'), here it works

标签: javascript jquery element hierarchy


【解决方案1】:

试试

$(carousel).find("#" + o.theid + " > .inside");

其中&gt; 表示子级。

【讨论】:

    【解决方案2】:

    你试过了吗:

    $carousel_container = $(carousel).find("#" + o.theid).children('.inside');
    

    【讨论】:

      猜你喜欢
      • 2021-06-07
      • 2012-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-26
      • 1970-01-01
      • 2020-04-25
      • 2016-07-24
      相关资源
      最近更新 更多