標籤:
屬性操作
<input type=”text” class=”apple” id=”username” name=”username” value=”tom” address=”beijing” />
itnode.屬性名稱
itnode.屬性名稱= 值;
itnode.getAttribute(屬性名稱);
itnode.setAttribute(屬性名稱,值);
jquery方式操作屬性(attribute):
$().attr(屬性名稱); //獲得屬性資訊值
$().attr(屬性名稱,值); //設定屬性的資訊
$().removeAttr(屬性名稱); //刪除屬性
$().attr(json對象); //同時為多個屬性設定資訊值,json對象的索引值對就是名稱和值
$().attr(屬性名稱,fn); //通過fn函數執行的return傳回值對屬性進行賦值
/**********************************************************************/
<script type="text/javascript">
function f1(){
//獲得屬性資訊
console.log($("#username").attr(‘type‘));
console.log($("#username").attr(‘class‘));
console.log($("#username").attr(‘id‘));
console.log($("#username").attr(‘name‘));
console.log($("#username").attr(‘value‘));
console.log($("#username").attr(‘address‘));
}
function f2(){
//修改屬性
$(‘#username‘).attr(‘class‘,‘orange‘);
$(‘#username‘).attr(‘name‘,‘email‘);
$(‘#username‘).attr(‘value‘,‘[email protected]‘);
$(‘#username‘).attr(‘address‘,‘shanghai‘);
$(‘#username‘).attr(‘weight‘,‘120‘);//新屬性
//$(‘#username‘).attr(‘type‘,‘radio‘); //禁止修改
$(‘#username‘).attr(‘id‘,‘email123‘);
}
function f3(){
//批量修改屬性
//$().attr(json對象);
var jn = {name:‘tel‘,‘class‘:‘pear‘,value:‘132787323728‘,address:‘瀋陽‘}
$(‘#username‘).attr(jn);
}
function f4(){
//函數傳回值設定屬性
$(‘#username‘).attr(‘value‘,function(){
return 10+30;
});
}
function f5(){
//刪除屬性
$(‘#username‘).removeAttr(‘class‘);
$(‘#username‘).removeAttr(‘name‘);
$(‘#username‘).removeAttr(‘value‘);
$(‘#username‘).removeAttr(‘address‘);
$(‘#username‘).removeAttr(‘id‘);
//$(‘#username‘).removeAttr(‘type‘); //禁止刪除
}
</script>
</head>
<body>
<h1>屬性操作</h1>
<input type="text" class="apple" id="username" name="username" value="tom" address="beijing" />
<input type="button" value="擷取" onclick="f1()" />
<input type="button" value="設定" onclick="f2()" />
<input type="button" value="批量修改" onclick="f3()" />
<input type="button" value="函數修改" onclick="f4()" />
<input type="button" value="刪除屬性" onclick="f5()" />
</body>
/**********************************************************************/
jQuery 屬性操作