javascript - 使用 jQuery 遍历并匹配元素

标签 javascript jquery jquery-traversing

我在单击标签行时克隆一个 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')")

关于javascript - 使用 jQuery 遍历并匹配元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22033872/

相关文章:

javascript - 如何检测来自javascript事件的输入更改

javascript - 使用 jquery 选择取消选择复选框

jquery - 如何在jquery中以百分比设置宽度

jquery - 在 jQuery 中的 .each() 中使用 .load() 方法不返回所有 html 内容

javascript - 是否有 jQuery 选择器来完成此任务?

javascript - 在 Cocos2d-JS 中使用 Google Play 服务

javascript - 从移动设备访问时 Bootstrap 导航栏不会折叠,当我缩小桌面浏览器时工作正常

java - 解码 javascript 编码的 url

jquery - 如何使用 jQuery 选择最接近的元素(不能是其父元素)?

jquery - 在 jQuery 中测试下一个元素