jQuery 知识点总结(更新中)

来源:互联网 发布:守望先锋查看队友数据 编辑:程序博客网 时间:2024/05/20 11:22

一、筛选
slice()
作用:函数用于选取匹配元素中一段连续的元素,并以jquery对象的形式返回
语法:jQueryObject.slice( startIndex [, endIndex ] )
startIndex 属于Integer类型,指定选取的起始索引,从0开始算起。
endIndex 可选/Integer类型,指定的结束索引(不包括该索引位置的元素)

实例解释说明:
html中代码:

<div id="n1">    <div id="n2">        <ul id="n3">            <li id="n4">item1</li>            <li id="n5" class="foo bar">item2</li>            <li id="n6">item3</li>            <li id="n7">item4</li>            <li id="n8">item5</li>        </ul>    </div></div>

jquery代码:

// 扩展jQuery对象,添加showTagInfo()方法// 用于将jQuery对象所有匹配元素的标识信息追加到body元素内// 每个元素的标识信息形如:"tagName"或"tagName#id"jQuery.fn.showTagInfo = function(){    var tags = this.map( function(){        return this.tagName + ( this.id ? "#" + this.id : "" );     } ).get();    $("body").append( tags.join("<br>") + "<br><br>" );};/* $("li") 匹配n4、n5、n6、n7、n8这5个元素 *///选取第2个元素$("li").slice( 1, 2).showTagInfo();  // LI#n5//选取第4、5个元素$("li").slice( 3 ).showTagInfo();// LI#n7,LI#n8//选取第1~4个元素//startIndex = length + (-5) = 0,endIndex = length + (-1) = 4$("li").slice( -5,  -1).showTagInfo( ); // LI#n4,LI#n5,LI#n6,LI#n7
0 0
原创粉丝点击