2015-05-14 48 views
1

出现问题。 在下面的小提琴中,我试图举例说明如何检查变量“htmlstring”是否已被追加,如果是,则不追加另一个变量。另一方面,如果它没有被追加,课程将追加它。检查变量是否已被追加

http://jsfiddle.net/Lc5gdvge/6/

如何使这项工作?总而言之,我需要的是一个if-else代码,它将检查以前的附加变量。如果没有被追加,它将追加,如果容器已经包含附加变量“htmlstring”,那么它当然不应该追加一个新的变量。

$(document).ready(function() { 
$('.outerdiv').click(function() { 
    var htmlstring = $(this).contents().filter(function(){return this.nodeType == 3;}).text() 
    if ($('.innerdiv').innerhtml(jQuery.inArray(htmlstring, arr))){ /*this line is the problem*/ 
    } 
    else { 
     $(this).find('div.innerdiv').append(htmlstring);} 

}) 
}); 
+2

丢失在哪里'ARR '被定义? – AlliterativeAlice

回答

1

您可以使用jQuery的length找到,如果任何内容存在像

var checkContent = $(this).find('div.innerdiv').html().length; 
if(checkContent> 0){ 
//contents are present 
} 

注:你有一个分号在

$('.outerdiv').click(function() { 

});// missing semi-colon. Its good to use one 
+0

也许我应该提到被检查的容器在实际项目中也包含了很多不同的标签。这有什么区别吗? –

+0

然后使用$(this).find('div.innerdiv')。children('你想要的标签')长度 – Abhinav

+0

不,不。我不需要检查标签,我只是想知道你的解决方案是否工作了事件htmlstring被附加到一个容器,其中还包含许多不同的标签。我已经用同一个容器中的元素的一个小例子更新了小提琴。 –