关于用javascript改变class所用的setAttribute()方法
2008-03-04 09:16
483 查看
需要注意的是:
1.element要用getElementById or ByTagName来得到,
2.setAttribute("class", vName)中class是指改变"class"这个属性,所以要带引号。
3.IE中要把class改成className,.....IE不认class,所以最好写两句,都用上吧。
W3C DOM - {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、setAttribute()的差异
我们经常需要在JavaScript中给Element动态添加各种属性,这可以通过使用setAttribute()来实现,这就涉及到了浏览器的兼容性问题。
var bar = document.getElementById("foo");
bar.setAttribute("onclick", "javascript:alert('This is a test!');");
这里利用setAttribute指定e的onclick属性,简单,很好理解。但是IE不支持,IE并不是不支持setAttribute这个函数,
而是不支持用setAttribute设置某些属性,例如对象属性、集合属性、事件属性,也就是说用setAttribute设置style和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!"); }
1.element要用getElementById or ByTagName来得到,
2.setAttribute("class", vName)中class是指改变"class"这个属性,所以要带引号。
3.IE中要把class改成className,.....IE不认class,所以最好写两句,都用上吧。
W3C DOM - {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、setAttribute()的差异
我们经常需要在JavaScript中给Element动态添加各种属性,这可以通过使用setAttribute()来实现,这就涉及到了浏览器的兼容性问题。
var bar = document.getElementById("foo");
bar.setAttribute("onclick", "javascript:alert('This is a test!');");
这里利用setAttribute指定e的onclick属性,简单,很好理解。但是IE不支持,IE并不是不支持setAttribute这个函数,
而是不支持用setAttribute设置某些属性,例如对象属性、集合属性、事件属性,也就是说用setAttribute设置style和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!"); }
相关文章推荐
- 关于用javascript改变class所用的setAttribute()方法
- 关于用javascript改变class所用的setAttribute()方法
- 关于用javascript改变class所用的setAttribute()方法
- 关于用javascript改变class所用的setAttribute()方法
- 关于用javascript改变class所用的setAttribute()方法
- 关于JavaScript的addClass,removeClass,hasClass方法
- JavaScript中关于class的调用方法
- 关于eclipse提示class file editor source not found的解决方法
- 关于javascript的childNodes方法的疑惑
- javascript不改变源码为原方法追加内容的方法
- 关于:Myeclipse工程重命名之后,发布,但是tomcat的webapps下没有改变的解决方法
- 关于phpcms中模块_tag.class.php中的pc_tag()方法的含义
- 关于spring boot方法queryByID(id)的java.lang.ClassCastException: A cannot be cast to A 报错
- JavaScript关于attachEvent和addEventListener的使用方法
- iBaits中,关于insert返回值的问题(注意!!!不必写resultClass= "java.lang.Integer",方法的返回值就是int)
- javascript中关于宽高、位置、坐标等相关属性和方法介绍:
- JavaScript中关于indexOf的使用方法与问题小结
- 关于AE中IFeatureWorkspace接口的CreateFeatureClass方法
- php面向对象中的单例与静态方法的对比,以及关于class自动加载的分析
- javascript检查表单数据是否改变的方法