js——each,eq,get

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)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章