作者:wasw100
网址:http://www.wasw100.com/html_js_css/jquery_attr_val.html
$(this).attr(key); 获取节点属性名的值,相当于getAttribute(key)方法
$(this).attr(key, value); 设置节点属性的值,相当于setAttribute(key,value)方法
$(this).val();获取某个元素节点的value值,相当于$(this).attr("value");
$(this).val(value);设置某个元素节点的value值,相当于$(this).attr("value",value);
jquery例子:
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jquery attr属性</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#iButton").click(function(){
alert($(this).attr("test")); //获取test的属性
$(this).attr("test","abc");//设置test的属性为abc
alert($(this).attr("test"));
});
});
</script>
</head>
<body>
<input id="iButton" type="button" test="123" value="按钮" />
</body>
</html>
对应的javascript dom代码为:
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>attribute属性</title>
<script type="text/javascript">
function attrTest(){
alert(document.getElementById("iButton").getAttribute("test"));
document.getElementById("iButton").setAttribute("test","abc");
alert(document.getElementById("iButton").getAttribute("test"));
}
</script>
</head>
<body>
<input id="iButton" type="button" test="123" value="按钮" οnclick="attrTest();" />
</body>
</html>
--EOF--