jQuery文件剖析4-屬性的動態設定

來源:互聯網
上載者:User

例子1:  動態設定img的src屬性  其他dom元素雷同 用於動態屬性的設定
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
   $(document).ready(
   function(){
    $("#load").click(function(){
   $("img").attr("src","admin.png") ;   //設定img的src屬性
   $("img").attr("src") ;//返回所有img的src屬性....
    }
    ) ;
    $("#unload").click(function(){
     $("img").removeAttr("src") ;  //刪除img的src屬性//
    }) ;
   }   
   ) ;
</script>
<title>Insert title here</title>
</head>
<body>
<img  src="">
<button id="load">顯示</button>
<button id="unload">刪除</button>
</body>
</html>
 
2、html text val的用法區別  ......
$('p').html();   是擷取所有p標籤下的html程式碼封裝括標籤....$('p').html("<a>xxx<a>"); 設定p下的html內容,和dom的innerHTML 屬性相對應   $('p').text( );         同上設定或者擷取..p標籤下的所有文本...或者設定 ....這列的<a>dxx</a>都會解析成文本 $("p").text("Hello world!");        $("input").val();     擷取或者設定 表單元素的值..具體可以結合篩選..選取器 選擇我們的元素然後再擷取或者設定值$("input").val("hello world!"); 3、通過動態設定dom元素的class 設定css屬性 <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">  www.2cto.com
   $(document).ready(
   function(){
    $("#load").click(function(){
   $("p").addClass("styleme") ;   //增加class屬性 styleme
    }
    ) ; $("#unload").click(function(){
     $("p").removeClass("styleme") ;  //卸載css屬性
    
    }) ;
   }   
   ) ;
</script>
<style type="text/css">
.styleme{
   font-size: 20px ;
   color: blue;
}
</style>
<title>Insert title here</title>
</head>
<body>
<p>23232d的</p>
<button id="load">顯示</button><button id="unload">卸載</button>

</body>
</html> $("p").toggleClass("selected");  //表示如果累不存在就添加一個類 4、設定/擷取在匹配的元素集中的第一個元素的屬性值。$("input[type='checkbox']").prop("checked"); 擷取input元素中type=checkbox 元素集合的第一個元素的checked屬性值$("input[type='checkbox']").prop("disabled", false);  禁用checkbox$("input[type='checkbox']").prop("checked", true);    設定checkbox選中、///  jQuery文檔中目前就介紹了這些屬性設定方法
 
 

作者:yue7603835

聯繫我們

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