jQuery 屬性操作

來源:互聯網
上載者:User

標籤:

 屬性操作

<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 屬性操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.