jQuery技术:jQuery元素索引

我有一个充满表格数据的表格。 我需要在表中找到列(单元格)的索引。

例如:

Column1 Column2 Column3
foo bar foobar
function TestIndexOf(someTD) { $(someTD) // what's my column index? }

    $('td').prevAll().length将给出基于0的单元$('td').prevAll().length

    或者使用index() (可以传递DOM元素或jQuery对象。如果是jQuery对象,则只使用包装集中的第一个对象)

     var cell = $('td'); // select on cell cell.parent().index(cell); 

    如果我没记错的话, index()将更容易在jQuery 1.4中使用,并且允许你简单地在包装在jQuery对象中的元素上调用index()来获取索引,就像这样

     $('td').index() // NOTE: This will not work in versions of jQuery less than 1.4 

    所以对你的function

     function TestIndexOf(someTD) { return $(someTD).prevAll().length; } 

      以上就是jQuery教程分享jQuery元素索引相关内容,想了解更多jQuery开发(异常处理)及jQuery教程关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/jquerytutorial/561505.html

      (0)
      上一篇 2021年2月4日
      下一篇 2021年2月4日

      精彩推荐