2011-03-04 83 views

回答

2
function removeElement(parentDiv, childDiv){ 
    if (childDiv == parentDiv) { 
      alert("The parent div cannot be removed."); 
    } 
    else if (document.getElementById(childDiv)) {  
      var child = document.getElementById(childDiv); 
      var parent = document.getElementById(parentDiv); 
      parent.removeChild(child); 
    } 
    else { 
      alert("Child div has already been removed or does not exist."); 
      return false; 
    } 
} 

removeElement('parent','child'); 
1

如果通過刪除您僅僅意味着隱藏,那麼你可以從內容腳本運行此:

document.querySelector('div.section1').style.display = 'none'; 

(假設只有1 section1頁面上的元素,否則你將需要使用document.querySelectorAll,並根據一些標準篩選結果)

+0

使用'querySelector' – Martijn 2011-03-05 11:25:45

3
function removeElementsByClassName(names) { 
    var els = document.getElementsByClassName(names), 
     i, element; 
    for (i = els.count - 1; i > 0; i -= 1) { 
     element = els[i]; 
     element.parentElement.removeChild(element); 
    } 
} 

removeElementsByClassName('section1'); 
相關問題