JavaScript之Dom操作【删除当前节点】

//最新更新:2017-11-25
//现在可以通过更强大而快捷的方式为所有的HTMLElement元素的Dom操作扩展新的方法【注意事项:处理HTMLElemnt元素时,此法对IE-8无效】
//原理:原型

(function(){
	HTMLElement.prototype.remove = function(){
		if(this.parentNode){
			this.parentNode.removeChild(this);
		}
	}
})();



//demo
var div = document.createElement("div");
div.setAttribute("id","demo");
div.innerText = "hello";
document.body.append(div);
//div.remove();

 


传统方式:
document.getElementById("test").parentNode.removeChild(document.getElementById("test"));

function removeById(Id){
    document.getElementById(Id).parentNode.removeChild(document.getElementById(Id))
}

 

function removeByClassName(classNames,indexs){

   for(var i = 0;i<classNames.length;i++){
       document.getElementsByClassName(classNames[i])[indexs[i]].parentNode.removeChild(document.getElementsByClassName(classNames[i])[indexs[i]]
); } }

 以清理百度文库为例:

step0:将文档全部页面都在网页上展开

step1:点击文库的全屏图标

step2:

clear();
function removeById(Id){
    document.getElementById(Id).parentNode.removeChild(document.getElementById(Id))
}
function removeByClassName(classNames,indexs){
   for(var i = 0;i<classNames.length;i++){
       document.getElementsByClassName(classNames[i])[indexs[i]].parentNode.removeChild(document.getElementsByClassName(classNames[i])[indexs[i]]); 
	} 
}
removeById("next_doc_box");
removeByClassName(["fix-searchbar-wrap","reader-tools-bar-wrap","pay-page-mod","ft","footer","new-ico-wkmember-free-doc"],[0,0,0,0,0,0]);

  

转载于:https://www.cnblogs.com/johnnyzen/p/7631544.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值