$(document).ready(function(){ });
$("p.intro") 選取所有 class="intro" 的 <p> 元素。
$("[href]") 選取所有帶有 href 屬性的元素。
$("[href='#']") 選取所有帶有 href 值等於 "#" 的元素。
$("[href!='#']") 選取所有帶有 href 值不等於 "#" 的元素。
jQuery 事件函數:對應$(selector).action()的后半部分。
<html> <head> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ $("p").hide(); }); }); </script> </head> <body> <h2>This is a heading</h2> <p>This is a paragraph.</p> <p>This is another paragraph.</p> <button>Click me</button> </body> </html>
Event 函數 | 綁定函數至 |
---|---|
$(document).ready(function) | 將函數綁定到文檔的就緒事件(當文檔完成加載時) |
$(selector).click(function) | 觸發或將函數綁定到被選元素的點擊事件 |
$(selector).dblclick(function) | 觸發或將函數綁定到被選元素的雙擊事件 |
$(selector).focus(function) | 觸發或將函數綁定到被選元素的獲得焦點事件 |
$(selector).mouseover(function) | 觸發或將函數綁定到被選元素的鼠標懸停事件 |
- 把所有 jQuery 代碼置於事件處理函數中
- 把所有事件處理函數置於文檔就緒事件處理器中
- 把 jQuery 代碼置於單獨的 .js 文件中
- 如果存在名稱沖突,則重命名 jQuery 庫
可選的 speed 參數規定隱藏/顯示的速度,可以取以下值:"slow"、"fast" 或毫秒。
<html>
<head>
<script src="/jquery/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button#yincang").click(function(){
$("p#id").hide(1000,function(){alert('bye')});
});
});
$(document).ready(function(){
$("button#xian").click(function(){
});
</script>
</head>
<body>
<button type="button" id='xian'>顯示</button>
<button type="button" id='yincang'>隱藏</button>
<p id="id">這是一個段落。</p>
<p>這是另一個段落。</p>
</body>
<html>
<head>
<script src="/jquery/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("p").toggle();
});
});
</script>
</head>
<body>
<button type="button">切換</button>
<p>這是一個段落。</p>
<p>這是另一個段落。</p>
</body>
jQuery 擁有下面四種 fade 方法:
- fadeIn()
- fadeOut()
- fadeToggle() jQuery fadeToggle() 方法可以在 fadeIn() 與 fadeOut() 方法之間進行切換。
- fadeTo() fadeTo() 方法允許漸變的最終結果為給定的不透明度(值介於 0 與 1 之間,0為透明,1為不透明)。
jQuery 滑動方法
通過 jQuery,您可以在元素上創建滑動效果。
jQuery 擁有以下滑動方法:
- slideDown()
- slideUp()
- slideToggle()
jQuery 動畫 - animate() 方法
$(selector).animate({params},speed,callback);
必需的 params 參數定義形成動畫的 CSS 屬性。
可選的 speed 參數規定效果的時長。它可以取以下值:"slow"、"fast" 或毫秒。
<head>
<script src="/jquery/jquery-1.11.1.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("div").animate({left:'250px'});
});
});
</script>
</head>
<body>
<button>開始動畫</button>
<div style="background:#98bf21;height:100px;width:100px;position:absolute;">
</div>
</body>
var div=$("div");
div.animate({height:'300px',opacity:'0.4'},"slow");
div.animate({width:'300px',opacity:'0.8'},"slow");
div.animate({height:'100px',opacity:'0.4'},"slow");
div.animate({width:'100px',opacity:'0.8'},"slow");
可選的 stopAll 參數規定是否應該清除動畫隊列。默認是 false,即僅停止活動的動畫,允許任何排入隊列的動畫向后執行。
<html>
<head>
<script src="/jquery/jquery-1.11.1.min.js"></script>
<script>
$(document).ready(function(){
$("#flip").click(function(){
$("#panel").slideDown(5000);
});
$("#stop").click(function(){
$("#panel").stop();
});
});
</script>
<style type="text/css">
#panel,#flip
{
padding:5px;
text-align:center;
background-color:#e5eecc;
border:solid 1px #c3c3c3;
}
#panel
{
padding:50px;
display:none;
}
</style>
</head>
<body>
<button id="stop">停止滑動</button>
<div id="flip">點擊這里,向下滑動面板</div>
<div id="panel">Hello world!</div>
</body>
alert("The paragraph is now hidden");
- text() - 設置或返回所選元素的文本內容
- html() - 設置或返回所選元素的內容(包括 HTML 標記)
- val() - 設置或返回表單字段的值
$("#btn1").click(function(){
alert("Text: " + $("#test").text());
});
$("#btn2").click(function(){
alert("HTML: " + $("#test").html());
});$("#btn1").click(function(){
alert("Value: " + $("#test").val());
});$("button").click(function(){
alert($("#w3s").attr("href"));
});
$("#test1").text("Hello world!");
});
$("#btn2").click(function(){
$("#test2").html("<b>Hello world!</b>");
});
$("#btn3").click(function(){
$("#test3").val("Dolly Duck");
$("#test1").text(function(i,origText){
return "Old text: " + origText + " New text: Hello world!
(index: " + i + ")";
});
});
$("#btn2").click(function(){
$("#test2").html(function(i,origText){
return "Old html: " + origText + " New html: Hello <b>world!</b>
(index: " + i + ")";
});
$("#w3s").attr("href", function(i,origValue){
return origValue + "/jquery";
});
- append() - 在被選元素的結尾插入內容
- prepend() - 在被選元素的開頭插入內容
- after() - 在被選元素之后插入內容
- before() - 在被選元素之前插入內容
如需刪除元素和內容,一般可使用以下兩個 jQuery 方法:
- remove() - 刪除被選元素(及其子元素)
- empty() - 從被選元素中刪除子元素
jQuery remove() 方法也可接受一個參數,允許您對被刪元素進行過濾。
<head>
<script src="/jquery/jquery-1.11.1.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").remove(".italic");
});
});
</script>
</head>
<body>
<p>This is a paragraph in the div.</p>
<p class="italic"><i>This is another paragraph in the div.</i></p>
<p class="italic"><i>This is another paragraph in the div.</i></p>
<button>刪除 class="italic" 的所有 p 元素</button>
</body>
jQuery 操作 CSS
jQuery 擁有若干進行 CSS 操作的方法。我們將學習下面這些:
- addClass() - 向被選元素添加一個或多個類
- removeClass() - 從被選元素刪除一個或多個類
- toggleClass() - 對被選元素進行添加/刪除類的切換操作
- css() - 設置或返回樣式屬性
<html>
<head>
<script src="/jquery/jquery-1.11.1.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("h1,h2,p").addClass("blue");
$("div").addClass("important");
});
});
</script>
<style type="text/css">
.important
{
font-weight:bold;
font-size:xx-large;
}
.blue
{
color:blue;
}
</style>
</head>
<body>
<h1>標題 1</h1>
<h2>標題 2</h2>
<p>這是一個段落。</p>
<p>這是另一個段落。</p>
<div>這是非常重要的文本!</div>
<br>
<button>向元素添加類</button>
</body>
$("#div1").addClass("important blue");
});
jQuery css() 方法
- width()
- height()
- innerWidth()
- innerHeight()
- outerWidth()
- outerHeight()
width() 方法設置或返回元素的寬度(不包括內邊距、邊框或外邊距)。
var txt="";
txt+="Width: " + $("#div1").width() + "</br>";
txt+="Height: " + $("#div1").height();
$("#div1").html(txt);
innerWidth() 方法返回元素的寬度(包括內邊距)。
outerWidth() 方法返回元素的寬度(包括內邊距和邊框)。
outerWidth(true) 方法返回元素的寬度(包括內邊距、邊框和外邊距)。
向上遍歷 DOM 樹
這些 jQuery 方法很有用,它們用於向上遍歷 DOM 樹:
- parent()
- parents()
- parentsUntil()
$("span").parent();
});
您也可以使用可選參數來過濾對祖先元素的搜索。
$("span").parents("ul");
});
parentsUntil() 方法返回介於兩個給定元素之間的所有祖先元素,不包括開始也不包括結尾。
$("span").parentsUntil("div");
});
向下遍歷 DOM 樹
下面是兩個用於向下遍歷 DOM 樹的 jQuery 方法:
- children()
- find()
jQuery children() 方法
$("div").find("span");
});
$("div").find("*");
有許多有用的方法讓我們在 DOM 樹進行水平遍歷:
- siblings()
- next()
- nextAll()
- nextUntil()
- prev()
- prevAll()
- prevUntil()
$("h2").siblings("p");
});
$("h2").next();
});
<html>
<head>
<script src="/jquery/jquery-1.11.1.min.js">
</script>
<script>
$(document).ready(function(){
$("div p").first().css("background-color","yellow");
});
</script>
</head>
<body>
<h1>歡迎來到我的主頁</h1>
<div>
<p>這是 div 中的另一個段落。</p>
</div>
<p>這也是段落。</p>
</body>
<head>
<script src="/jquery/jquery-1.11.1.min.js">
</script>
<script>
$(document).ready(function(){
$("p").eq(1).css("background-color","yellow");
});
</script>
</head>
<body>
<h1>歡迎來到我的主頁</h1>
<p>我最好的朋友是米老鼠 (index 3)。</p>
</body>
<head>
<script src="/jquery/jquery-1.11.1.min.js">
</script>
<script>
$(document).ready(function(){
$("p").filter(".intro").css("background-color","yellow");
});
</script>
</head>
<body>
<h1>歡迎來到我的主頁</h1>
<p>我是唐老鴨。</p>
<p class="intro">我住在 Duckburg。</p>
<p class="intro">我愛 Duckburg。</p>
<p>我最好的朋友是 Mickey。</p>
</body>