簡體   English   中英

遍歷jQuery和匹配元素

[英]Traversing with jQuery and matching elements

我在點擊標記行時克隆一個div(帶有h3,一個段落和一個可點擊的標記行),並將div附加到我的側邊欄。 有幾個具有相同結構的div,當div已經被克隆時,我想確保第二次沒有克隆該div。 為了實現這一點,我試圖匹配div的H3文本,其中使用已經克隆的div的H3文本單擊了其標記行。 如果匹配,我會彈出一條警告消息,並且不會將克隆的div附加到側欄。

這是我的代碼:

$(this).click(function(){ // where $(this) is the tag line

var clone = $(this).parents('.full_result').clone(); // on the click of the tag line, find the parent whose class is .full_result and clones it (.full_result is the class of all divs)

var jobsH3 = $(this).parents('.full_result').find('h3').text(); // returns the text of the H3 that is contained in the same div as the clicked tag line

var middleColumnInnerDiv=$('#middle_column').find('.full_result').find('h3').text(); // returns the text of all h3 whose divs have been cloned to the side bar(sidebar id= #middle_column)

//下面是魔法應該發生的地方,但我不能讓它發揮作用。 嘗試了幾種選擇器和方法。 :contains只是其中之一。

  $(this).parents('.full_result').find('h3').each(function(){
      if('middleColumnInnerDiv:contains(jobsH3)'){ // this line is giving me a headache
     alert('You already saved this information');   
      } else {
           clone.appendTo('#middle_column').hide().fadeIn(750);
    }               
   });
};

任何幫助深表感謝!

你缺少$ Jquery表示法。 試試這個$("middleColumnInnerDiv:contains('.jobsH3')")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM