HTML标签可以自定义属性,但是我们要考虑其在IE和Firefox下的兼容性问题。
<input type="text" id="tt" name="mm" value="" outerAttribute="fdfdfdfdf" />
这里的outerAttribute属性是我们自定义的,在javaScript中如何引用这个值呢?可以使用
注:outerAttribute可以是其他的自定义的属性名称
document.getElementByIdx_x_x("tt").outerAttribute
上面的代码只在IE下有效。要想兼容Firefox则需要使用下面的代码
document.getElementByIdx_x_x("tt").getAttribute("outerAttribute")
一般情况下,自定义属性主要是为了存储一些和当前标签相关联的一些重要的值,在web页面设计中可以考虑。
例:
<input type="text" id="ceshi" name="ceshir" dataType="phone" msg="电话不能为空" /> alert(document.getElementByIdx_x_x("ceshi").getAttribute("dataType")); alert(document.getElementByIdx_x_x("ceshi").getAttribute("msg")); <script type="text/javascript"> function test(){ var d1 = document.getElementByIdx_x('d1'); var p1 = document.getElementByIdx_x('p1'); alert(d1.alt); // 仅IE支持 alert(p1.alt); // 仅IE支持 alert(d1.getAttribute('alt')); // IE、FF、谷歌浏览器都支持 alert(p1.getAttribute('alt')); // IE、FF、谷歌浏览器都支持 } </script> <div id="d1" alt='ddd_1_2'>测试一</div> <p id="p1" alt='ppp_1_2'>测试二</p> <input type="button" value="测试" onclick="test()" />