button被div嵌套后 无法获得button的索引值

xin_dian 2017-12-29 03:27:26
...全文
408 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ice1682937 2018-01-05
  • 打赏
  • 举报
回复
<ul> <li id="foo">foo</li> <li id="bar">bar</li> <li id="baz">baz</li> </ul> $('#bar').index(); //1,不传递参数,返回这个元素在同辈中的索引位置。 !!!!!!! $('li').index(document.getElementById('bar')); //1,传递一个DOM对象,返回这个对象在原先集合中的索引位置 $('li').index($('#bar')); //1,传递一个jQuery对象 $('li').index($('li:gt(0)')); //1,传递一组jQuery对象,返回这个对象中第一个元素在原先集合中的索引位置 $('#bar').index('li'); //1,传递一个选择器,返回#bar在所有li中的做引位置
qq_39718591 2018-01-05
  • 打赏
  • 举报
回复
注意你得选择器,$(this)是获取本身,你点击的是一个按钮,获得本身就只是获得了你点击的按钮,是无法获得所有按钮的。 <!doctype html> <html> <head> <title>test</title> <script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.min.js"></script> <script type="text/javascript"> $(function(){ $(".btn_a").click(function(){ var btn_n=$(".btn_a").index(this); console.log(btn_n); alert(btn_n); }) }) </script> </head> <body> <div> <button class="btn_a">1</button> </div> <div> <button class="btn_a">2</button> </div> </body> </html>
Go 旅城通票 2018-01-05
  • 打赏
  • 举报
回复
$(this).parent().index()

Web开发学习资料推荐
ajax对象属性withCredentials
Web前端开发
qq_31835601 2018-01-03
  • 打赏
  • 举报
回复
添加点击事件需要便利button添加
天际的海浪 2017-12-29
  • 打赏
  • 举报
回复
$(this).index(".change"); 或者 $(".change").index(this);

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧