JavaScript 的setAttribute兼容性解决
setAttribute各个浏览器都支持,但在IE7以下版本中,有些属性值还是有差异的,比如
- obj.setAttribute("class","classname")
在ie8等主流浏览器能起效,但在IE7以下版本中不起作用,因为IE7以下版本不认得“class”,他们只认得“className”;
单一的兼容性可以这样写
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Document</title>
- <style>
- .test{
- width: 100px;
- height: 100px;
- background: blue;
- }
- .turn{
- background: red;
- width: 200px;
- height: 300px;
- }
- </style>
- <script>
- window.onload = function() {
- document.getElementsByTagName('button')[0].onclick = function() {
- var div = document.getElementsByTagName('div')[0]
- if(div.getAttribute("class")){//判断是否存在
- div.setAttribute("class","turn");/*IE8,chrome*/
- }else{
- div.setAttribute("className","turn")/*IE7-5*/
- }
- }
- }
- </script>
- </head>
- <body>
- <button>点击切换class</button>
- <div class="test"></div>
- </body>
- </html>
以上代码可以让setAttribute的class兼容所有主流浏览器
但除了class有差异外,还有下列属性也有差异
- class
- for
- cellspacing
- cellpadding
- tabindex
- readonly
- maxlength
- rowspan
- colspan
- usemap
- frameborder
- contenteditable
- style
为了解决上述问题就要写一个通用的跨浏览器的设置元素属性的接口方法:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset=" utf-8">
- <style type="text/css">
- .textcolor{
- font-size:18px;
- color:red;
- }
- </style>
- <script type="text/javascript">
- dom=(function(){
- var fixAttr={
- tabindex:'tabIndex',
- readonly:'readOnly',
- 'for':'htmlFor',
- 'class':'className',
- maxlength:'maxLength',
- cellspacing:'cellSpacing',
- cellpadding:'cellPadding',
- rowspan:'rowSpan',
- colspan:'colSpan',
- usemap:'useMap',
- frameborder:'frameBorder',
- contenteditable:'contentEditable'
- },
- div=document.createElement('div');
- div.setAttribute('class','t');
- var supportSetAttr = div.className === 't';
- return {
- setAttr:function(el, name, val){
- el.setAttribute(supportSetAttr ? name : (fixAttr[name] || name), val);
- },
- getAttr:function(el, name){
- return el.getAttribute(supportSetAttr ? name : (fixAttr[name] || name));
- }
- }
- })();
- window.onload=function(){
- var mydiv=document.getElementById("mydiv");
- dom.setAttr(mydiv, 'class', 'textcolor');
- }
- </script>
- </head>
- <body>
- </body>
- </html>
另外,同样可以使用element.style的方式去更改
例如
- document.getElementById("testbt").className = "bordercss";
- document.getElementById("testbt").style.cssText = "color: #00f;";
参考自:http://www.jb51.net/article/69685.htm
JavaScript 的setAttribute兼容性解决的更多相关文章
- AngularJS进阶(三十五)浏览器兼容性解决之道
浏览器兼容性解决之道 前言 浏览器兼容性一直是前端开发中不得不面对的一个问题.而最突出的就是IE.对绝大多数公司来说,兼容IE6的性价比已经很低,而IE7则几乎已经绝迹.所以,常见的兼容性下限是IE8 ...
- Javascript selection的兼容性写法介绍
本文为大家讲解下Javascript selection的兼容性写法,感兴趣的朋友可以参考下 function getSelectedText() { //this function code is ...
- <转>JavaScript的IE和火狐的兼容性解决办法
原文发布时间为:2009-05-06 -- 来源于本人的百度文章 [由搬家工具导入] 1. document.form.item 问题 (1)现有问题: 现有代码中存在许多 document.form ...
- Javascript事件机制兼容性解决方案
本文的解决方案可以用于Javascript native对象和宿主对象(dom元素),通过以下的方式来绑定和触发事件: 或者 var input = document.getElementsByTag ...
- Javascript 多浏览器兼容性问题及解决方案
一.document.formName.item(”itemName”) 问题 问题说明:IE下,可以使用 document.formName.item(”itemName”) 或 document. ...
- border-radius,box-shadow兼容性解决办法
css3 border-radius不支持IE8/IE7的四种解决方法 标签: cssborder-radius兼容性 时间:2016-07-18 css3 border-radius用于设置HT ...
- javascript获取style兼容性问题
获取css 样式的方法有三种 : style, currentStyle , getComputedStyle style (无兼容性问题) 获取语法: ele.style.attr : 设置语法:e ...
- 不同浏览器之间的javascript和css兼容性问题
po主手头维护的网站是上世纪的作品.当时约摸ie所占的市场份额相当大,以至于开发人员都没有考虑到浏览器兼容性问题(这不科学!).怎奈po主是个强迫症阿.最近在修改的时候,还是没忍住,把兼容性问题解决了 ...
- WCF不支持 ASP.NET 兼容性 解决办法
错 误提示:无法激活服务,因为它不支持 ASP.NET 兼容性.已为此应用程序启用了 ASP.NET 兼容性.请在 web.config 中关闭 ASP.NET 兼容性模式或将 AspNetCompa ...
随机推荐
- 【转】eclipse -- the project was not built due to a resource exists with a different case...
原文网址:http://blog.csdn.net/mylinx/article/details/44280563 进行编码时,工程前面莫名有个红X,正当百思不得其解时,发现在[problems]下有 ...
- uri编解码
相关函数如下:(都是全局函数) encodeURI(URIString):将文本字符串编码为有效的统一资源标示符URI decodeURI(URIString) encodeURIComponent( ...
- 奔跑的xiaodao
http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=2086 很明显的一个二分题目.因为要 ...
- 在mac中用终端来运行.c文件
第一步:打开终端,位置在lauchpad中去找搜索. 第二步:建一个.c文件. 第三步: 在终端输入.c路径.用cd命令 第五步:cc -c +tab键.生成.O文件 第六步:cc +tab键.生成. ...
- 动态规划——A 最大子段和
A - 最大子段和 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit St ...
- poj2299
好吧,看到这个图片就知道是干什么的了,求逆序数- - 可以用线段树,貌似还可以用归并排序,这题应该是考的归并排序,毕竟是递归分治- - 基本上都忘了,再写一写试试吧. AC ///////////// ...
- oGitHub 注册
GitHub 注册 要想使用 GitHub 第一步当然是注册 GitHub 账号: 1.首先打开 https://github.com/pricing 进行注册. 2.在打开的页面中点击「Sign u ...
- php数组和字符串转换
PHP 中由于数组和字符串这两种变量类型是如此常用,以至于 PHP 具有两个函数,可以在字符串和数组之间互相进行转换. $array=explode(separator,$string); $stri ...
- OneToMany与ManyToOne的属性
供自己查阅,嫌低级的勿喷! 1.OneToMany的属性 ①targetEntity 定义关系类的类型,默认是该成员属性对应的类类型,所以通常不需要提供定义. ②mappedBy 定义类之间的双向关系 ...
- nginx 配置 开发
1 .安装: 2.修改配置文件nginx.conf 添加server: