2010-11-12 61 views
1

我有一个指南,其中每个章节都位于UL的单独LI中。我正在尝试使用jQuery Clone函数来搜索包含所有这些'章节'LI的父UL,并返回包含特定文本的章节。选择要使用jQuery复制的元素克隆

现在,我得到的结果很奇怪,可能是因为它是在最小的孩子身上复制元素,而不是整个div。

此外,这些章节LI中的每一个都应该只返回一次。

  • makeIntoSldieshowUL - UL,包含所有 '章节'
  • slideShowSlide - 类名称为每个 '章'
  • searchResultsArea - 事业部在其中添加 '章节' 包含文本

所以远我有:

$("#makeIntoSlideshowUL").find(".slideShowSlide:contains('" + $(this).val() + "')").clone().appendTo("#searchResultsArea"); 

为了让你知道我想克隆的内容,这里是一个简短的样本

<ul id="makeIntoSlideshowUL"> 
<li class="slideShowSlide" id="0"> 
    <div class="topicTitle">Cardholder responsibilities</div> 
    <p>Cardholders are responsible for ensuring proper use of the card. If your division or department has approved you for a Pro-Card, you must use the card responsibly in accordance with the following requirements:</p> 
    <ul> 
     <li>Purchase items for UCSC business use only</li> 
     <li>Never lend or share your Pro-Card</li> 
     <li>Purchase only allowable goods and services</li> 
    </ul> 
</li> 
<li class="slideShowSlide" id="1"> 
    <div class="topicTitle"><strong>Restricted and Unallowable Pro-Card Purchases</strong></div> 
    <p>Some types of purchases are restricted are not allowed with the Pro-Card. Disputes with suppliers are initially handled by the Cardholder if, for example, they don't recognize a transaction on their statement, or the amount doesn't match their receipt. The Cardholder is responsible for contacting the supplier immediately to try to resolve the matter.</p> 
</li> 

+0

您能给出您正在使用的实际标记的样本/标本吗? – 2010-11-12 22:35:18

回答

1

使用jQuery's .children() method而不是.find(),因为它听起来就像是.slideShowSlide元素是直接的孩子。可以使用the > child selector代替。

$("#makeIntoSlideshowUL > .slideShowSlide:contains('" + $(this).val() + "')").clone().appendTo("#searchResultsArea"); 

编辑:在一个点上,你似乎指的是章节,申报单。如果他们是<li>元素的孩子,你可能会需要这样的东西:

$("#makeIntoSlideshowUL > li > .slideShowSlide:contains('"... 

+0

他们实际上是列表元素,而不是div。我猜想习惯的力量。我更新了我的问题以反映这一点。 – 2010-11-12 22:48:24

+0

我也应该注意到,我试图将LI复制到一个div中。我已经完成并将#searchREsultsArea更改为UL元素。感谢您阅读我的双重参考文献,它帮助我发现了这个错误。 – 2010-11-12 22:52:00

+0

@Eric - 所以我认为前两种解决方案适合你?如果没有,请告诉我。此外,仅供参考,HTML4中无法使用以数字开头的ID。他们必须从一封信开始。 :O) – user113716 2010-11-12 23:00:14

0

尝试使用the拥有或contains选择

has不会改变当前元素jQuery堆栈。

$("#makeIntoSlideshowUL") 
    .has(".slideShowSlide:contains('" + $(this).val() + "')") 
    .clone() 
    .appendTo("#searchResultsArea");