示例 1 : 获取
<script src="http://how2j.cn/study/jquery.min.js"></script>
<script>
$(function(){
$("#b1").click(function(){
alert("align属性是:" + $("#h").attr("align") );
});
$("#b2").click(function(){
alert("game属性是:" + $("#h").attr("game") );
});
});
</script>
<button id="b1">获取align属性</button>
<button id="b2">获取自定义属性 game</button>
<br>
<br>
<h1 id="h" align="center" game="LOL">居中标题</h1>
示例 2 : 修改
<script src="http://how2j.cn/study/jquery.min.js"></script>
<script>
$(function(){
$("#b1").click(function(){
$("#h").attr("align","right") ;
});
});
</script>
<button id="b1">修改align属性为right</button>
<br>
<br>
<h1 id="h" align="center" >居中标题</h1>
示例 3 : 删除
<script src="http://how2j.cn/study/jquery.min.js"></script>
<script>
$(function(){
$("#b1").click(function(){
$("#h").removeAttr("align");
});
});
</script>
<button id="b1">删除align属性</button>
<br>
<br>
<h1 id="h" align="center" game="LOL">居中标题</h1>
示例 4 : prop与attr的区别
<script src="http://how2j.cn/study/jquery.min.js"></script>
<script>
$(function(){
$("#b1").click(function(){
alert("game属性是:" + $("#c").attr("game") );
});
$("#b2").click(function(){
alert("game属性是:" + $("#c").prop("game") );
});
$("#b3").click(function(){
alert("checked属性是:" + $("#c").attr("checked") );
});
$("#b4").click(function(){
alert("checked属性是:" + $("#c").prop("checked") );
});
});
</script>
<style>
button{
display:block;
}
</style>
<button id="b1">通过attr获取自定义属性 game</button>
<button id="b2">通过prop获取自定义属性 game</button>
<button id="b3">通过attr获取 checked属性</button>
<button id="b4">通过prop获取 checked属性</button>
<br>
<br>
<input type="checkbox" id="c" game="LOL" checked="checked"> 选中的复选框