jQuery學習筆記

來源:互聯網
上載者:User

標籤:style   blog   color   java   io   strong   檔案   div   cti   

一、jQuery選取器:

1、jQuery元素選取器  

$("p.text") //選取所有class="text"的<p>元素
$("p#text") //選取所有id="text"的<p>元素 

2、jQuery屬性選取器

$("[href]")     //選取所有帶有 href 屬性的元素。$("[href=‘#‘]")   //選取所有帶有 href 值等於 "#" 的元素。$("[href!=‘#‘]")   //選取所有帶有 href 值不等於 "#" 的元素。$("[href$=‘.jpg‘]") //選取所有 href 值以 ".jpg" 結尾的元素。 

3、jQuery CSS選取器

$("p").css("background-color","red");  //老是忘記括起red的引號!!!

 

二、事件:

$(document).ready(function(){
  //some code
})

  ready 表示文檔結構已經載入完成(不包含圖片等非文字媒體檔案),與其相對的是onload,指示頁麵包含圖片等檔案在內的所有元素都載入完成。

$(selector).click(function(){  //some code})

  click是比較常用的事件,觸發或將函數綁定到被選元素的點擊事件。還有dbclick(雙擊)、mouseover(懸浮)、focus(獲得焦點)等等。

 

三、獲得內容和屬性( text()、html()、val()、attr() )

1、text() - 設定或返回所選元素的常值內容

  類似於javascript中的innerText屬性,可以用來讀取或者設定某個元素的常值內容。

<p><strong>Hello world</strong></p>alert($("p").text());  //Hello world

2、html() -設定或返回所選元素的內容(包括 HTML 標籤)

  類似於javascript中的innerHtml屬性,可以用來讀取或者設定某個元素中的HTML內容。

 

<p><strong>Hello world</strong></p>alert($("p").html());  // <strong>Hello world</strong>

3、val() - 設定或返回表單欄位的值

  類似於javascript中的value屬性,可以用來設定或者擷取元素的值。

<p>Name: <input type="text" name="user" value="Hello World" /></p>alert($("[type=‘text‘]").val()); //  Hello World

4、attr() -用於擷取、設定屬性值

<p id="p1" name="content">good night</p>alert($("#p1").attr("name"));   //content

$(selector).attr(attribute,value) //設定屬性值
$("img").attr("width","180");

  

 

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.