我FF测试结果: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3typeof truegetAttribute truehasAttribute truehasOwnProperty false
不需要啊... http://www.w3.org/TR/1998/REC-DOM-Level-1-19981001/level-one-core.html#ID-666EE0F9在JavaScript 权威指南(中文第五版)p783~784也有写 Element.getAttribute() 返回指定属性的字符串值 描述 方法getAttribute( )将返回一个元素的指定属性的值。注意,HTMLElement 对象定义了和每个标准 HTML 属性对应的JavaScript属性,因此,只有当你查询非标准属性的值的时候,才需要和HTML文档一起使用此方法。 在XML文档中,属性值不能直接做为元素属性,必须通过调用方法来查询它们。对于使用名字空间的XML文档来说,需要使用getAttributeNS( )方法。 例子 接下来的代码说明了两种获取HTML<img>元素的性质值的方法: // Get all images in the document var images = document.body.getElementsByTagName("img"); // Get the src attribute of the first one var src0 = images[0].getAttribute("src"); // Get the src attribute of the second simply by reading the property var src1 = images[1].src;
var images = document.body.getElementsByTagName("img"); // Get the src attribute of the first one var src0 = images[0].getAttribute("src"); // Get the src attribute of the second simply by reading the property var src1 = images[1].src;这个肯定有的,<img src="11.jpg">谁都会把src属性写在标签上,这样getAttribute就能拿到。
IE中(IE9没测试)Element没有hasOwnProperty倒是真的
abc.setAttribute('checked',true);
你这样赋值,看看还返回false吗
你看看这样能否达到想要的效果(我上面测试的几个浏览器,除了IE、IE7外,其余全灭)
abc.setAttribute('checked',true);
里面写的是true
Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3typeof truegetAttribute truehasAttribute truehasOwnProperty false
http://www.w3.org/TR/1998/REC-DOM-Level-1-19981001/level-one-core.html#ID-666EE0F9在JavaScript 权威指南(中文第五版)p783~784也有写
Element.getAttribute()
返回指定属性的字符串值
描述
方法getAttribute( )将返回一个元素的指定属性的值。注意,HTMLElement 对象定义了和每个标准 HTML 属性对应的JavaScript属性,因此,只有当你查询非标准属性的值的时候,才需要和HTML文档一起使用此方法。
在XML文档中,属性值不能直接做为元素属性,必须通过调用方法来查询它们。对于使用名字空间的XML文档来说,需要使用getAttributeNS( )方法。
例子
接下来的代码说明了两种获取HTML<img>元素的性质值的方法:
// Get all images in the document
var images = document.body.getElementsByTagName("img");
// Get the src attribute of the first one
var src0 = images[0].getAttribute("src");
// Get the src attribute of the second simply by reading the property
var src1 = images[1].src;
// Get the src attribute of the first one
var src0 = images[0].getAttribute("src");
// Get the src attribute of the second simply by reading the property
var src1 = images[1].src;这个肯定有的,<img src="11.jpg">谁都会把src属性写在标签上,这样getAttribute就能拿到。
而你这个测试,是在赋值。
但......
abc.readOnly = true;
alert(abc.getAttribute("readOnly"));
这样却也可以取到值,浏览器是FF3.6.18
如果照说getAttribute只能取到setAttrbiute设置的值和直接写在标签里的属性,那上面这代码又是.......
呃~·我也还真说不清楚这个是为什么。
我理解的话,就是不管拿什么属性或者赋值什么属性。最好都用Attribute这个。
为了更好的兼容
是不是
function hasAttr(element, name) {
return element.attributes[name] !== undefined;
}
不过这貌似和我顶楼代码的第一个判断是一样的啊... typeof element[name] !== "undefined"
在FF 3.6.16(自己的笔记本是这个版本)中
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" language="javascript">
// <![CDATA[
window.onload = function() {
var abc = document.getElementById("abc");
abc.checked = false;
alert(hasAttr(abc, "checked"));
};
function hasAttr(element, name) {
//return element.attributes[name] !== undefined; // FF中既然返回false,IE8照样返回true
return typeof element[name] !== "undefined"; // 2者都返回true
}
// ]]>
</script>
</head>
<body>
<input type="checkbox" name="abc[]" id="abc" />
</body>
</html>
http://bbs.blueidea.com/thread-3028850-1-1.html