Check if element is before or after another element in jQuery

后端 未结 9 1591
花落未央
花落未央 2020-12-23 16:23

Let us suppose I have this markup

First

Hi

Hello

Second

9条回答
  •  南笙
    南笙 (楼主)
    2020-12-23 17:10

    Here is a fiddle

    $('p').each(function() {
        previousH3 = $(this).prevAll('h3');
        nextH3 = $(this).nextAll('h3');
    
        if (previousH3.length > 0 && nextH3.length > 0) {
            $(this).css('color', 'red')
        }
    
    });
    

提交回复
热议问题