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