each和.is
html
<button>Change colors</button>
<span></span>
<div></div>
<div></div>
<div></div>
<div></div>
<div id="stop">Stop here</div>
<div></div>
<div></div>
<div></div>
js
$(function () {
$("button").click(function (e) {
//e是jQuery對象`在這裏插入代碼片`
// $(this) == $(e.target)一樣
$("div").each(function (index,domEle) {
console.log(domEle, $(this));
// domEle 和 this使用一樣
//this是jQuery對象;domEle是dom元素
$(this).css("backgroundColor", "yellow");
if ($(this).is("#stop")) {
$("span").text("Stopped at div index #" + index);
return false;
}
});
});
});
get和eq
html
<img src="test1.jpg"/>
<img src="test2.jpg"/>
js
//get取得所有匹配的 DOM 元素集合
$("img").get(0)
//eq 返回的是jQuery對象。
$("img").eq(0)