zoukankan      html  css  js  c++  java
  • setAttribute的兼容性

    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!); }

  • 相关阅读:
    详解SQL Server的两个存储过程:sp_MSforeachtable/sp_MSforeachdb
    使用WorkService创建定时任务
    Mahout下个性化推荐引擎Taste介绍
    Apache Mahout中的机器学习算法集
    内网信息收集笔记 楼下的小可怜
    关于linux的suid提权 楼下的小可怜
    Cobalt Strike初探 楼下的小可怜
    Google hacking 楼下的小可怜
    Git和Repo扫盲——如何取得Android源代码 zt
    Howto find native code memory leak in Android
  • 原文地址:https://www.cnblogs.com/dunken/p/4401699.html
Copyright © 2011-2022 走看看