jQuery .prev() of a type regardless of it's parent etc

后端 未结 3 1005
遇见更好的自我
遇见更好的自我 2021-01-27 14:38

is there a simple way to get the previous occurrence of an element in the DOM? If I\'m looking at #text3 and I want to get ahold of the previous input #text2<

3条回答
  •  广开言路
    2021-01-27 15:08

    You can use $.index(), like so:

    var previous = null;
    var inputs = $('input');
    var index = inputs.index(this);
    if (index > 0) {
        previous = $(inputs[index-1])
    }
    

    For example: http://jsfiddle.net/pYaBL/1/

提交回复
热议问题