Find the position of an element within a list

后端 未结 4 762
太阳男子
太阳男子 2021-01-12 00:08

I\'m looking to find the position (i.e. the order) of a clicked element within a list using jQuery.

I have:

  • Element 1
4条回答
  •  误落风尘
    2021-01-12 00:48

    Can also be done with plain JavaScript:

    let = _this = this,
          indx = Array.prototype.slice.call(_this.parentElement.childNodes).indexOf(_this);
    

    Or, if you trying to style your ordered html list with other than CSS like


    , as @Jan mentioned, use querySelectorAll('li') instead of childNodes.

提交回复
热议问题