日期:2014-05-17  浏览次数:20693 次

javascript/js获取html元素自定义属性的值(兼容Firefox和IE)
<table>
	<tr eventId=356 ondblclick="f(this)">
		<td style="position:relative">	
			<input value="bbb" selft="abc" onclick="f(this)">
		</td>
		<td style="position:relative">	
			aaa
		</td>
	</tr>
</table>
<script>
	function f(ele){
		// alert(ele.eventId);// 这个只能在 IE中可用
		// 下面这两种方法在IE,FireFox和Chrome中都能用,都可以
		alert(ele.attributes['eventId'].nodeValue);
		alert(ele.getAttribute('eventId'));
		// 总结,对于HTML元素,标准中规定的属性可以 ele.attributeName
		// 的方法去获取其值;用户自定义的(自动放到页面元素中的)属性
		// 就要通过ele.getAttribute("attributeName") 的方式来获取其值
		// 了。[IE例外] :)
	}
</script>

严重参考:http://blog.csdn.net/xxtjp/article/details/6852496