我正在尝试克隆并附加一个div到另一个div。但我只想在目标div不包含克隆时移动它。
function addToPortFolio(cl) {
var className = cl.attr('id');
console.log(className);
if ($(".portfolioWrapper").has("#" + className).length > 0){
console.log('exists');
} else {
cl.clone().appendTo('.portfolioWrapper');
console.log('not exist');
}
}
下面是控制台日志:
not exist
AAA.L
not exist
AAA.L
not exist
AAAP
not exist
AAAP
exists
AAAP
exists
AAAP
exists
它的行为很奇怪,也不知道为什么。如果我连续点击div与类AAAP它不会工作,但AAAP会?
这是一个普遍的坏习惯吗?
应该可以:
function addToPortFolio(cl) {
if ($(".portfolioWrapper").find($('.' + cl.className.replace(' ','.'))).length > 0){
console.log('exists');
} else {
cl.clone().appendTo('.portfolioWrapper');
console.log('just added it');
}
}