Logo

javascript移除标签所有属性

photo

2022年04月29日

 
今天在学校的一个页面里,发现此页面竟然把右键单击给禁用了。
遂翻出页面代码,发现只是<body>节点里简单的加入了

  1. oncontextmenu="return false" onselectstart="return false" 
  • 1.
 
Remove tag’s all attributtes;

  1. function removeAllAttr(id){ 
  2.     // var node = document.getElementsByTagName(tag)[0]; 
  3.     var node = document.getElementsById(id); 
  4.     if(node){    
  5.     var attr = node.attributes;  
  6.     while(attr.length){  
  7.       node.removeAttribute(attr[attr.length-1].nodeName)  
  8.      }  
  9.     } 
  10. }  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

 

= = = = = = = = = = = = = = = = = =

使用jQuery的方法(网上copy的):

  1. $("selector").each(function() { 
  2.   // first copy the attributes to remove 
  3.   // if we don't do this it causes problems 
  4.   // iterating over the array we're removing 
  5.   // elements from 
  6.   var attributes = $.map(this.attributes, function(item) { 
  7.     return item.name; 
  8.   }); 
  9.  
  10.   // now use jQuery to remove the attributes 
  11.   var tag = $(this); 
  12.   $.each(attributes, function(i, item) { 
  13.     tag.removeAttr(item); 
  14.   }); 
  15. }); 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

 

所有附件
该文章没有附件.
本文为原创文章,请注意保留出处!

留言板

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

写在前面我花了将近一个多月的时间断断续续写下这篇博文,并精心写下完整demo,所以转载务必保留h...Chrome插件(扩展)开发全攻略

热门文章

修复群晖Synology Drive client右键菜单缺失问题 本教程主要解决windows10右键菜单中没有SynologyDrive菜单的问题,整体思路是找到...修复群晖SynologyDriveclient右键菜单缺失问题 作者:Pastore Antonio
1816 浏览量
docker如何查看一个镜像内部的目录结构及其内部都有哪些文件 前言:有时候我们会在docker上下载一个镜像,或者是上传一个镜像到docker上,甚至有时候就是在...docker如何查看一个镜像内部的目录结构及其内部都有哪些文件 作者:Pastore Antonio
1801 浏览量
configure: error: Package requirements (oniguruma) were not met configure:error:Packagerequirements(oniguruma)...configure:error:Packagerequirements(oniguruma)werenotmet 作者:Pastore Antonio
1532 浏览量
Adobe Acrobat Pro 激活 这里记录了一些AdobeAcrobat的激活教程和组件。浏览量:1,685 作者:Pastore Antonio
1531 浏览量
追寻日出,找回自己 为什么我要去追寻日出?其实我是一个很懒的人,每次都起不来,直到有一次我在租房中睡到了大天亮,阳光照...追寻日出,找回自己 作者:Pastore Antonio
1508 浏览量