Javascript 如何使用Chrome扩展删除站点中的div?

Javascript 如何使用Chrome扩展删除站点中的div?,javascript,google-chrome-extension,Javascript,Google Chrome Extension,站点中有一个div: <div class="section1"> .... </div> .... 我想用Chrome扩展删除它。。。有人能单独给出javascript代码吗?谢谢。如果删除意味着隐藏,那么您可以从内容脚本运行此操作: function removeElement(parentDiv, childDiv){ if (childDiv == parentDiv) { alert("The parent div canno

站点中有一个div:

<div class="section1">
....
</div>

....

我想用Chrome扩展删除它。。。有人能单独给出javascript代码吗?谢谢。

如果删除意味着隐藏,那么您可以从内容脚本运行此操作:

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');
document.querySelector('div.section1').style.display = 'none';

(这假设页面上只有1个
section1
元素,否则您需要使用
document.querySelectorAll
并根据某些条件过滤结果)

如果删除仅意味着隐藏,则可以从内容脚本运行此操作:

document.querySelector('div.section1').style.display = 'none';
(这假设页面上只有1个
section1
元素,否则需要使用
document.querySelectorAll
并根据某些条件过滤结果)