How to implement “prevUntil” in Vanilla JavaScript without libraries?

前端 未结 7 1055
遇见更好的自我
遇见更好的自我 2020-12-29 11:32

I need to implement the functionality of jQuery\'s prevUntil() method in Vanilla JavaScript.

I\'ve got several

elements on the same level:
7条回答
  •  醉话见心
    2020-12-29 12:03

    Use .children in combination with .parentNode. Then filter the NodeList, after converting it into an array: http://jsfiddle.net/pimvdb/DYSAm/.

    var div = document.getElementsByTagName('div')[0];
    var siblings = [].slice.call(div.parentNode.children) // convert to array
                     .filter(function(v) { return v !== div }); // remove element itself
    console.log(siblings);
    

提交回复
热议问题