JQuery基礎文法小結

來源:互聯網
上載者:User

JQuery基礎文法小結

 在日常開發中JQuery是我們使用最頻繁的JS庫。使用JQuery進行開發,你需要先瞭解JQuery的基本文法。下面是在學習JQuery時總結的一些簡單文法。

 

 

1、$(document)將document對象轉換為jquery

 

代碼如下:


$(document).ready(function(){
alert("hello world");
});

 

2、擷取所有的超連結對象,並且增加onclick事件;其實在底層jquery對象獲得的每個標籤的數組,因此我們不需要進行迴圈了

 

代碼如下:


$(document).ready(function(){
$("a").click(function(){
alert("hello world");
});
});

 

3、jquery對象與dom對象之間的轉換

 

代碼如下:


$(document).ready(function(){
var javascriptElement = document.getElementById("clickme");
//將dom對象轉換為jquery對象
var jqueryElement = $(javascriptElement);
//alert("javascript:" + javascriptElement.innerHTML);//innerHTML擷取標籤的內容
//alert("jquery:" + jqueryElement.html());
//將jquery轉換為dom對象方式一
var jElement = $("#clickme");
var javascriptEle = jElement[0];
alert("jquery1: " + javascriptEle.innerHTML);
//方式二
var javascriptEle2 = jElement.get(0);
alert("jquery2: " + javascriptEle2.innerHTML);
});

 

4、jquery解決id值是否存在的問題

 

代碼如下:


<a id="hello">click me</a>
<script type="text/javascript">
//以傳統方式解決id為空白方式一
if(document.getElementById("helllo")) {
//如果hello不存在會出錯
document.getElementById("helllo").style.color ="red";
}
//方式二
//$("#hello")[0].style.color="red";
//用jquery解決
$("#hello").css("color","red");
//如果只有一個參數,則此方法是讀,如果是兩個參數則是些功能(css(“”),css(“”,“”))
alert($("#hello").css("color"));
</script>

 

5、在javascript中規定,你要操作css屬性時要將屬性中的‘-'去掉,同時把後一個字母大寫;如:background-color 要改成 backgroundColor

6、jquery中也使用選取器來操縱各種各樣的元素。是繼承了css的設計理念,但是把它更是發揚光大了;

7、jquery的幾種書寫形式

1> 方式一

 

代碼如下:


$("document").ready(function(){
...
});

 

2> 方式二

 

代碼如下:


$().ready(function(){
...
});

 

3> 方式三

 

代碼如下:


$(function(){
...
});

 

以上幾種方式是一樣的

希望本文所述對大家學習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.