class和className兼容方法:

object.setAttribute("class","content")

在IE8、Chrome、火狐、Opera10中都能设置成功;但是在IE7下无法设置。

object.setAttribute("className","content")

只有IE7能设置成功,但是其他浏览器均无法设置。

兼容方法:

使用 object.className="content"

style和cssText兼容方法:

object.setAttribute("style","position:absolute;left:10px;top:10px;")

在IE8、Chrome、火狐、Opera10中都能设置成功;但是在IE7下无法设置。

object.setAttribute("cssText","position:absolute;left:10px;top:10px;")

此设置方法,所有浏览器均不支持。

兼容方法:

使用 object.style.cssText="position:absolute;left:10px;top:10px;"

或者单独 object.style.各个属性 ,逐一进行设置。

Firefox和IE的JS兼容性:设置元素style熟悉

在IE下setAttribute设置元素的对象、集合和事件属性都只当成普通属性,起不到原有的作用,但可以直接进行赋值操作,如下:

var cssText = ”font-weight:bold;color:red;”
 //下面写法用于firefox类型浏览器
element.setAttribute(“style”,cssText);

//下面写法用于IE类型浏览器
element.style.cssText = cssText;

方法属性等问题
例如:
var bar =
document.getElementById(testbt);
bar.setAttribute(onclick,
javascript:alert('This is a
test!'););
这里利用setAttribute指定e的onclick属性,简单,很好理解。
但是IE不支持,IE并不是不支持setAttribute这个函数,而是不支持用setAttribute设置某些属性,例如对象属性、集合属性、事件属性,也就是说用setAttribute设置style和onclick这些属性在IE中是行不通的。

为达到兼容各种浏览器的效果,可以用点符号法来设置Element的对象属性、集合属性和事件属性。
document.getElementById(testbt).className
= bordercss;
document.getElementById(testbt).style.cssText = color:
#00f;;
document.getElementById(testbt).style.color =
#00f;
document.getElementById(testbt).onclick= function () { alert(This is a
test!); }

setAttribute的兼容性的更多相关文章

  1. js中setAttribute 的兼容性

    js中setAttribute 的兼容性class和className兼容方法: object.setAttribute("class","content") ...

  2. JS中setAttribute的兼容性问题(摘自leejersey)

    class和className兼容方法: object.setAttribute("class","content") 在IE8.Chrome.火狐.Opera ...

  3. setAttribute一个兼容性问题

    前几天工作中遇到一个js问题,本来js就不大会,倒腾了好长时间,并在做弹窗的时候用到了setAttribute,出现了不兼容的问题,在网上查了好多,真是郁闷,看来啥都得学啊. 主要的工作是做一个根据时 ...

  4. JS setAttribute兼容

    问题和表现: 最近实践中遇到的问题,setAttribute()设置在IE7中,无法设置style等属性.这样就对设置样式带了很大的困扰,例如绑定点击事件来隐藏元素,setAttribute(”sty ...

  5. JavaScript 的setAttribute兼容性解决

    setAttribute各个浏览器都支持,但在IE7以下版本中,有些属性值还是有差异的,比如 obj.setAttribute("class","classname&qu ...

  6. setAttribute的浏览器兼容性(转)

    1.element要用getElementById or ByTagName来得到, 2.setAttribute("class", vName)中class是指改变"c ...

  7. setAttribute的浏览器兼容性

    1.element要用getElementById 或者是ByTagName来得到 2.setAttribute("class", vName)中class是指改变"cl ...

  8. SVG中 transform矩阵遇到的兼容性问题

    SVG  transform矩阵遇到的兼容性问题.在chrome.safari.火狐.360极速浏览器上都正常显示的图,在手机端就不行啊!!! 先上图. 图1     PC端浏览器 图2   iPho ...

  9. js在IE和FF下的兼容性问题

    本文出自前端档案,以作学习参考之用.自己也补充了一些内容 长久以来JavaScript兼容性一直是Web开发者的一个主要问题.在正式规范.事实标准以及各种实现之间的存在的差异让许多开发者日夜煎熬.为此 ...

随机推荐

  1. Learning BSD.sys/queue.h

    This file includes 4 data-structures.. Insteresting because they are written in 1994.. to make it ea ...

  2. gridControl 中CellValueChanged,ShowingEditor,CustomDrawCell的用法

    private void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventA ...

  3. ios给textView提价提示性文字

    不推荐使用的方式 但是在用的时候才发现原来textView没有类似于textField的那种placeholder功能.所谓placeholder就比如用户看到一个输入框,然后输入框里面一般会有几个浅 ...

  4. VB.net结束进程

    Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ...

  5. Android面试题集锦 (转)

    转自:http://xiechengfa.iteye.com/blog/1044721 一些常见的Android面试基础题做下总结,看看你能做出多少道? 1. Intent的几种有关Activity启 ...

  6. JavaScript高级程序设计:第三章

    基本概念 一.语法: 1.区分大小写: 2.标识符:指变量.函数.属性的名字,或者函数的参数.标识符可以是按照下列格式规则组合起来的一个或多个字符: (1)第一个字符必须是一个字母.下划线(_).或者 ...

  7. Bank Interest

    Bank Interest Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Tota ...

  8. dip2px

    package com.itheima.zhbj.utils; import android.content.Context; public class DensityUtils { public s ...

  9. windows指令

    &        无条件执行&符号后面的命令: &&      当&&前面的命令成功执行时,执行&&后面的命令,否则不执行: ||   ...

  10. android cordova h5总结

    最近项目 替换页面  把80%页面替换成h5了. 首页h5页面可以放在android本地.增加访问速度.节省用户流量 把服务器上的  js代码 压缩成zip格式  放在asset目录.当应用安装时候 ...