JavaScript setAttribute 兼容性处理指南

作者:异步漫步

时间:2026-10-30

浏览:572

本文深入分析JavaScript中setAttribute方法的浏览器兼容性问题。重点讲解在Firefox与IE内核浏览器中,class属性需分别使用setAttribute("class")和直接赋值className的区别,以及处理onclick等事件属性时,IE不支持setAttribute设置事件属性的替代方案。通过代码示例展示如何编写跨浏览器的DOM操作代码,确保样式和事件绑定的正确执行。

文章目录

JavaScript setAttribute 兼容性处理指南

本文通过具体实例,详细解析了JavaScript中setAttribute方法的兼容性问题,旨在为开发者提供实用的参考与解决方案。

1:常规属性建议使用 node.XXXX。 2:自定义属性建议使用node.getAttribute("XXXX")。 3:当获取的目标是JS里的关键字时建议使用node.getAttribute("XXX"),如label中的for。 4:当获取的目标是保留字,如:class,请使用className代替。

setAttribute(string name, string value)方法用于添加一个指定名称和值的新属性,或将现有属性设定为指定值。

样式属性设置问题

1、样式属性设置问题

在setAttribute("class", value)中,class作为属性名需要加引号,以明确指定修改的是"class"属性。

其中vName代表要赋值的样式名称。

具体示例如下:

代码实现:


var input = document.createElement("input");
input.setAttribute("type", "text");
input.setAttribute("name", "q");
input.setAttribute("class",bordercss);

执行后,生成的HTML代码为:<input type="text" name="q" class="bordercss">,即input控件成功应用了bordercss样式。

需要注意的是,虽然class属性在W3C DOM标准中地位重要,但不同浏览器对其实现存在差异。

使用setAttribute("class", vName)动态设置Element的class属性在Firefox中可以正常工作,但在IE内核浏览器中却无效。这是因为IE内核不识别"class"属性名,必须改用"className"。

反之,Firefox也不识别"className"属性名。因此,通用的兼容写法需要同时兼顾两者:

代码实现:


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

方法与事件属性设置问题

2、方法与事件属性设置问题

例如:

代码实现:


var bar = document.getElementById("testbt");
bar.setAttribute("onclick", "javascript:alert('This is a test!');");

此处利用setAttribute指定元素的onclick属性,逻辑简单且易于理解。

然而,IE浏览器并不支持这种方式。IE并非完全不支持setAttribute函数,而是不支持通过setAttribute设置某些特定类型的属性,包括对象属性、集合属性和事件属性。这意味着在IE中使用setAttribute设置style或onclick等属性是无效的。

为了实现跨浏览器的兼容效果,建议使用点符号法来设置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!"); }

完整示例:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>kingwell</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<body>
<div id="idHeader" class="class-header" title="kingwell" status="1"></div>
<label id="forUserName" for="userName" title="kingwell" status="1"></label>
<script type="text/javascript">
var el = document.getElementById("idHeader");
alert(el.getAttribute("id"));
alert(el.id);
// IE Firfox->idHeader
alert(el.getAttribute("class"));
//IE6,IE7 -> null IE8,IE9,Firefox ->class-header
alert(el.class);
//IE6,IE7,IE8->报错 IE9,Firefox->undefined
alert(el.getAttribute("className"));
//IE6,IE7->class-header ; IE8,IE9,Firefox -> undefined
alert(el.className);
//All -> class-header
var elfor = document.getElementById("forUserName");
alert(elfor.getAttribute("for"));
//IE6,IE7->undefined IE8,9,Firefox->forUseName
alert(elfor.for )
//IE6,IE7报错,其它为undefined
alert(elfor.title)
//全部输出kingwell
alert(elfor.status);
//IE6-8 -> 1 IE9,Firefox->undefined
alert(elfor.getAttribute("status"))
//全部输出 1
</script>
</body>
</html>

如需了解更多JavaScript相关知识,可参考本站系列教程:《JavaScript常用函数技巧汇总》、《javascript面向对象入门教程》、《JavaScript中json操作技巧总结》、《JavaScript切换特效与技巧总结》、《JavaScript查找算法技巧总结》、《JavaScript动画特效与技巧汇总》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结》。

希望本文的阐述能对各位JavaScript程序员的开发工作有所帮助。

实用工具推荐

电子包浆

绿化、压缩、噪点和虚拟水印,自由调节做旧程度。

使用 ›
图片压缩

选择画质和输出格式,预览压缩前后的大小。

使用 ›
相关文章