2013-04-09 56 views
1

我正在使用javascript和jQuery來創建燈箱,並且我有一個簡單的函數,它將每個圖像封裝在一個容器div中,並帶有一個列表項,但是我想要檢查它們是否已經包裝在列表項中。有誰知道有辦法做到這一點?jQuery /檢查圖像是否包含在列表項中

事情是這樣的:

$('img, div.LBcontainer').each(function() { 
    //other stuff 
    if ($(this).parent() = li) { // Not sure of the syntax for it 
     $img.unwrap(); 
    } 
    else { 
     $img.wrap('<li id="images_LB"></li>'); 
    } 
}); 

回答

3

你可以簡單地檢查父和其長度:

if ($(this).parent("li").length) { 
    // parent is li 
} 
else { 
    // wrap 
} 
+0

我想這會是簡單的東西這樣。 謝謝! – EmmaGamma 2013-04-09 04:00:43

+0

http://stackoverflow.com/questions/3547677/jquery-check-if-parent-has-an-id – 2013-04-09 04:02:54

+0

對於驅動器,我很好奇上面的例子有什麼問題? – pickypg 2013-04-09 04:05:20