javascript获取节点的value,浏览器不兼容
代码如下
HTML code
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
<script type="text/javascript">
function fun(id){
var e = document.getElementById(id);
var v = e.value;
alert(v);
}
</script>
</head>
<body>
<div onclick="fun(this.id)" id="id" style="border:green; width:100px;height=50px; clear:both;" value="click here">click here</div>
</body>
</html>
在ie中点击那段文字 可以出现提示click here ,但是在火狐浏览器里边却出现undefined,这是怎么一回事啊??
------解决方案--------------------<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
<script type="text/javascript">
function fun(id){
var e = document.getElementById(id);
var v = e.getAttribute("value");
alert(v);
}
</script>
</head>
<body>
<div onClick="fun(this.id)" id="id" style="border:green; width:100px;height=50px; clear:both;" value="click here">click here</div>
</body>
</html>
这样试试
------解决方案--------------------1#的正确
自定义属性使用object.getAttribute("属性名称")获取,value不是div的标准属性,属于自定义属性