小白教程
所有教程
关于
Search
172.69.58.116
172.69.58.116
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Misc index”的源代码
本页内容
上一节:
Misc_get
下一节:
Misc_noconflict
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:index()}}[[Category:jquery ref misc|4]] = jQuery 杂项 index() 方法 = [[jquery ref misc|jQuery 杂项方法]] 获得被点击的 <li> 元素相对于它的同级元素的 index: <sample title="" desc="" lang="html" hererun="1"> $("li").click(function(){ alert($(this).index()); }); </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("li").click(function(){ alert($(this).index()); }); }); </script> </head> <body> <p>点击获取列表项的索引位置,相对于它的兄弟元素</p> <ul> <li>Coffee</li> <li>Milk</li> <li>Soda</li> </ul> </body> </html> </run> == 定义和用法 == index() 方法返回指定元素相对于其他指定元素的 index 位置。 这些元素可通过 jQuery 选择器或 DOM 元素来指定。 '''注意:'''如果未找到元素,index() 将返回 -1。 == 第一个匹配元素相对于同级元素的 index。 == 获得第一个匹配元素相对于其同级元素的 index 位置。 == 语法 == <sample title="" desc="" lang="html" hererun="1"> $(selector).index() </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("li").click(function(){ alert($(this).index()); }); }); </script> </head> <body> <p>点击获取列表项的索引位置,相对于它的兄弟元素</p> <ul> <li>Coffee</li> <li>Milk</li> <li>Soda</li> </ul> </body> </html> </run> == 元素相对于选择器的 index。 == 获得元素相对于选择器的 index 位置。 该元素可以通过 DOM 元素或 jQuery 选择器来指定。 == 语法 == <sample title="" desc="" lang="html" hererun="1"> $(selector).index(element) </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ alert($(".hot").index($("#favorite"))); }); }); </script> </head> <body> <p>单击按钮来获得id =“favorite”的元素的索引,相对于jQuery选择器(类=“hot”):</p> <button>获取下标</button> <ul> <li>Milk</li> <li class="hot">Tea</li> <li class="hot" id="favorite">Coffee</li> </ul> </body> </html> </run> {| class="table table-striped table-hover" ! 参数 ! 描述 |- | element | 可选。规定要获得 index 位置的元素。可以是 DOM 元素或 jQuery 选择器。 |} [[jquery ref misc|jQuery 杂项方法]]
返回至“
Misc index
”。
上一节:
Misc_get
下一节:
Misc_noconflict