zoukankan      html  css  js  c++  java
  • setAttribute()使用方法与IE兼容解决方法

    我们经常需要在JavaScript中给Element动态添加各种属性,可以使用setAttribute()来实现,但涉及到了浏览器的兼容性问题。
    setAttribute(string name,string value):增加一个指定名称和值的新属性,或者把一个现有的属性设定为指定的值。
    1、关于class和className
    class属性在W3C DOM中扮演着很重要的角色,但由于浏览器差异性仍然存在。使用setAttribute("class", vName)语句动态设置Element的class属性在firefox中是行的通的,在IE中却不行。因为使用IE内核的浏览器不认识"class",要改用"className";
    同样,firefox 也不认识"className"。所以常用的方法是二者兼备:

    element.setAttribute("class", vName);
    element.setAttribute("className", vName);  //for IE

    2、onclick
    bar.setAttribute("onclick", "");这里利用setAttribute指定onclick属性,简单很好理解。但是IE不支持。
    为达到兼容各种浏览器的效果,可以用点符号来设置Element的对象属性、集合属性和事件属性。

    document.getElementById("foo").className = "fruit";
    document.getElementById("foo").style.cssText = "color: #00f;";
    document.getElementById("foo").style.color = "#00f";
    document.getElementById("foo").onclick= function () { alert("This is a test!"); }
  • 相关阅读:
    【C++】<bits/stdc++.h>
    【网易】被3整除
    【TCP/IP详解】UDP:用户数据报协议
    【网易】牛牛找工作
    【C++】<numeric>中iota函数:递增序列填充

    今天好像找到C语言延迟输出的原因了
    C语言第2
    c语言新知
    glade No package 'libxml-2.0' found
  • 原文地址:https://www.cnblogs.com/AngelLee2009/p/3381030.html
Copyright © 2011-2022 走看看