JQuery 學習筆記 element屬性控制

來源:互聯網
上載者:User

複製代碼 代碼如下:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="js/jquery-1.3.2.js" ></script>
<script type="text/javascript"><!--
$(function(){
$("#aAttr1").click(function(){
$( ":text").attr("value",$("#divTest").attr("id"));
})
$("#aAttr2").click(function(){
$( ":text").attr("value",function(){
return $("#divTest").attr("id");
});
})
$("#aAttr3").click(function(){
$( ":text").attr({value:"test2"});
})
$("#aAttr4").click(function(){
$("#inputTest2").removeAttr("value");
})

})
// --></script>
<title>無標題文檔</title>
</head>

<body>
<form>
<input id="inputTest1" type="text" />
<input id="inputTest2" type="text" value="test" />
<div id="divTest">123</div>
<a href="#" id="aAttr1">在text表單中顯示DIV的ID方法1</a>|
<a href="#" id="aAttr2">在text表單中顯示DIV的ID方法2</a>|
<a href="#" id="aAttr3">在text表單中顯示test2</a>|
<a href="#" id="aAttr4">去除inputTest2的value屬性</a>|
<input type="reset" />
</form>
</body>
</html>

1.element.attr(name)
描述:用於擷取某個元素的name屬性值,如例子中 $("#divTest").attr("id")就可擷取divTest的ID值。
2.element.attr(name,value)
描述:用於設定某個元素的name屬性值,如例子中$(":text").attr("value",$("#divTest").attr("id"))就將divTest的ID值賦於text表單的value值。
註:例子中(":text")用於擷取input表單type為text的元素,同樣其它表單也可用同樣的方法擷取,如<input type="button"/>即可用$(":button")進行擷取,其傳回值為Array(Element),也可用$(":input")擷取所有的input元素。。在JQuery中要擷取和設定元素的text與value值還可以使用element.text()/element.text(value)、element.val()/element.val(Value),用法與element.html()一樣,若有疑問可以跟帖,我再做說明

3.element.attr(name,function)
描述:用於設定某個元素的name屬性值與上一個類似,只是這裡的value可以寫成個function,更為靈活。
4.element.remove(name);
描述:用於刪除某個元素的name屬性。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.