2011-04-11 23 views
0

我擁有包含imgs的div。但是,這些div中的一些也具有跨度作爲div內的第一個元素。此跨度代表此div內imgs的「標籤」。如果容器擁有某個類別的div,請從容器中移除img

我有一個腳本去如果我按住ALT並點擊這個'包'div,它會被刪除。但是,我不想刪除包含標籤的數據包。對於那些,只刪除內部的img就足夠了。

我在建立邏輯時遇到了麻煩。同時,通過點擊並按住alt鍵來傳達刪除的能力,我試圖讓一個css屬性改變一些顏色作爲提示。這也是行不通的。

$(".Card_Packet").click(function (e) { 
    if (e.altKey) { 

     $(this).live('hover', function(event) { 
      if (event.type === 'mouseover') { 
       $(this).css({ 
        'border-color': 'red' 
       }); 
      } 
      else { 
       $(this).css({ 
        'border-color':'black' 
       }); 
      } 
     }); 

     if ($(this).find('Sect_Header')) { //if the div contains the label, remove only the images. Otherwise, remove the whole thing. 
      $(this).remove('img'); 
     } else { 
      $(this).remove(); 
     } 
    } 
}); 

我確定它與我的操作順序有關。

+0

定義「不工作」 – Alnitak 2011-04-11 21:27:27

+3

應該是'.find(「 Sect_Header」)'的點缺 – Adam 2011-04-11 21:29:23

+0

什麼是‘Sect_Header’?它看起來像你正在尋找一個標籤名稱爲'Sect_Header'的元素。 – ChrisThompson 2011-04-11 21:31:20

回答

0
if($(this).children(":first").is("span")){ //If the div contains the label, remove only the images.. 
    $(this).find('img').remove(); 
}else{ //Otherwise remove packet div 
    $(this).remove(); 
} 
0

這裏是一些工作代碼:

$(".Card_Packet").click(function (e) { 

    if (e.altKey) { 

     if ($('.Sect_Header', this).length > 0) { 
      //if the div contains the label, remove only the images. 
      // Otherwise, remove the whole thing. 

      $('img', this).remove(); //removes the images 

     } else { 

      $(this).remove(); //removes the whole thing 

     } 

    } 

}); 

小提琴:http://jsfiddle.net/maniator/xwmwD/

0

你在做什麼會刪除整個股利。只是這樣做從任何DIV只刪除圖片:

$('select-your-div img').remove(); 
相關問題