选择要使用jQuery Clone复制的元素

时间:2010-11-12 22:30:59

标签: javascript jquery clone

我有一个指南,其中每章都位于UL的单独LI中。我正在尝试使用jQuery Clone函数来搜索包含所有这些“章节”LI的父UL,并返回包含特定文本的章节。

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

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

  • makeIntoSldieshowUL - 包含所有“章节”的UL
  • slideShowSlide - 每个“章节”的类名
  • searchResultsArea - 要附加包含文本的“章节”的Div

到目前为止,我有:

$("#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>

2 个答案:

答案 0 :(得分:1)

使用jQuery's .children() method代替.find(),因为.slideShowSlide元素听起来像是直接的孩子。

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

或者您可以使用the > child selector代替。

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

编辑:有一次,您似乎将这些章节称为div。如果他们是<li>元素的孩子,您可能需要以下内容:

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

答案 1 :(得分:0)

尝试使用the has或contains选择器

has不会更改jquery堆栈上的当前元素。

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