- $(document).ready(function(){
- jQuery.fn.showTags = function (n) {
- var tags = this.map(function () {
- return this.tagName;
- })
- .get().join(", ");
- $("b:eq(" + n + ")").text(tags);
- return this;
- };
- $("p").showTags(0)
- .find("span")
- .showTags(1)
- .css("background", "yellow")
- .end()
- .showTags(2)
- .css("font-style", "italic");
- });
- <p>
- Hi there <span>how</span> are you <span>doing</span>?
- </p>
- <p>
- This <span>span</span> is one of
- several <span>spans</span> in this
- <span>sentence</span>.
- </p>
- <div>
- Tags in jQuery object initially: <b></b>
- </div>
- <div>
- Tags in jQuery object after find: <b></b>
- </div>
- <div>
- Tags in jQuery object after end: <b></b>
- </div>
end()
将返回到执行find("span")操作之前的匹配元素集合状态
$("div").find("p").andSelf().addClass("border");
将匹配div元素集合增加到匹配p元素集合中。
本博客迁往 http://huangmin.sinaapp.com/ 敬请移步