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 ...
随机推荐
- 「Poetize7」足球比赛
描述 Description SJZEZ和TSYZ正在进行一轮足球联谊赛,根据规则,这轮比赛有两场,一场在SJZEZ的主场进行,一场在TSYZ的主场进行.胜负判断标准如下:1.在两场比赛中进球总数较多 ...
- Qt入门(11)——Qt插件
Qt提供了一个简单地插件接口,可以轻松地生成作为独立组件的定制数据库驱动.图象格式.文本编解码器(text codec).风格(style)和部件.警告:Qt 3.0.5对插件的一些方面做了改变,具体 ...
- (转载)PHP源代码分析- tick(s)
(转载)http://bbs.phpchina.com/forum.php?mod=viewthread&tid=94534 昨天有位朋友在杭州的PHPer群里面贴出了下面的一段代码并给出了运 ...
- 字符串(后缀自动机):NOI 2016 优秀的拆分
[问题描述] 如果一个字符串可以被拆分为 AABB 的形式,其中 A 和 B 是任意非空字符串, 则我们称该字符串的这种拆分是优秀的. 例如,对于字符串 aabaabaa,如果令 A = aab, B ...
- 【模拟】CSU 1807 最长上升子序列~ (2016湖南省第十二届大学生计算机程序设计竞赛)
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1807 题目大意: 给你一个长度为N(N<=105)的数列,数列中的0可以被其他数 ...
- IIS6.0服务器搭建网站无法访问解决方法
IIS6.0服务器搭建网站无法访问解决方法 IIS6.0服务器搭建网站无法访问解决方法很多朋友在用IIS6架网站的时候遇到不少问题,而这些问题有些在过去的IIS5里面就遇到过,有些是新出来的, ...
- Selenium firefox 版本问题
问题:Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms 原因: selenium-server-standalone-x. ...
- GCD中有哪几种Queue?你自己建立过串行Queue吗?背后的线程模型是什么样的
一共有五种,看图 Paste_Image.png 主线程也就是那个main,一般后台处理数据就就用default那个.创建过一个queue,处理NSMutableArray的时候都在在这一个queue ...
- 程序启动原理和UIApplication
iOS开发UI篇—程序启动原理和UIApplication 一.UIApplication 1.简单介绍 (1)UIApplication对象是应用程序的象征,一个UIApplication对象就 ...
- scrollview中停止滚动的监听
[补充]可以在主线程控制,特别注意 scrollView3.postDelayed(new Runnable() { @Override public void run() { scrollView3 ...