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

前端 未结 7 1058
遇见更好的自我
遇见更好的自我 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:08

    How about this:

    while ( node = node.previousElementSibling ) {
        if ( ( ' ' + node.className + ' ' ).indexOf( 'foo' ) !== -1 ) {
            // found; do your thing
            break;
        }
    }
    

    Don't bother telling me that this doesn't work in IE8...

提交回复
热议问题