2010-11-12 62 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");