Jquery First appearance of a tree bypassing parents / siblings

What is the best way to get the closest (to the tree) given by the class, but it can be a parent.

I want the element "errors1" to start with the element ".start"

<div>
    ...
    <div class="errors">errors2</div>
    <div>
        .....
        <div class="errors">errors1</div>
        ...
        <div>...</div>
        <div>
            .....
            <div class="start"></div>            
        </div>     
    </div>
</div>

Made a script, but it is very bad and does not work at all.

http://jsfiddle.net/9DfCJ/1/

thank

Editorial: Added "..." to increase the dynamic structure. The point is to find the closest .errors tree up.

+3
source share
6 answers

. . .start, .errors. .errors, .start.

jQuery(function($){

    var $start = $('.start'),
        $parents = $start.parents();

    $parents.each(function(){
        var $this = $(this),
            $thisErrors = $this.find('.errors'),
            numErrors = $thisErrors.length;

        if(numErrors){
            alert($thisErrors.eq(numErrors-1).text());
            return false;
        }

    });

});
+1

jQuery prev(). , "", "start" :

$('.start').click(function(){
  $(this).prev('.errors').hide();
});

: http://api.jquery.com/prev/

UPDATE:

, parent() , . parent() ... node.

http://api.jquery.com/parent/

+1
$(this).parent().siblings(':first')
+1

, jQuery, .

, Byran, , find (http://api.jquery.com/find/) .

, javascript script jQuery.

$('.start').closestParentsAndSibling( '.errors').html('found!');

(function( $ ){
   $.fn.closestParentsAndSibling = function(search) {
        // Get the current element siblings
        var siblings = this.siblings(search);

        if (siblings.length != 0) { // Did we get a hit?
            return siblings.eq(0);
        }

        // Traverse up another level
        var parent = this.parent();
        if (parent === undefined || parent.get(0).tagName.toLowerCase() == 'body') {
            // We reached the body tag or failed to get a parent with no result.
            // Return the empty siblings tag so as to return an empty jQuery object.
            return siblings;
        }
        // Try again
        return parent.closestParentsAndSibling(search);
   }; 
})( jQuery );
+1

, - :

var start = $(".start"),
    current = start.parent(),
    parent = current,
    end_class = "stop",
    get_errors = function(elt) {
        return elt.children(".errors");
    },
    errors = get_errors(current)

    while (parent = current.parent()) {

        $.merge(errors, get_errors(parent));
        if (parent.hasClass(end_class)) {
           break;
        }   
        current = parent;
    }

, errors .

end_class div.

<div class="stop">
    ...
    <div class="errors">errors2</div>
    ...
</div>
0

-, , , :

var myText = $(".start").closest($(".errors")).text(); 

more about this here , and you can use your event to call this code.

0
source

Source: https://habr.com/ru/post/1792141/


All Articles