简体   繁体   中英

jquery tree traversal issue

$('.field').blur(function() 
     $('*').not('.adress').click(function(e) {
                foo = $(this).data('events').click;
                if(foo.length <= 1) {
    //             $(this').next('.spacer').children().removeClass("visible");
                }
                $(this).unbind(e);
        });
});

I'm trying to remove a class "visible, whenever I'm bluring a field classed .field, unless if I click an element with the class .adress.

The field blur and adress-click is working as it should(I tired it with alert) but not the removal of class, does anyone know why?

The removal of class works if the not(".adress")-function is removed! Like this:

$('.field').blur(function() {
   (this).next('.spacer').children().removeClass("visible");
});

you've got an extra quote after //$(this . you don't need to quote the this variable.

Additionally, your * selector can be simplified to $(':not(.address)')

Can you provide an example of your HTML?

I'm trying to remove a class "visible, whenever I'm bluring a field classed .field, unless if I click an element with the class .adress.

This is quite tricky. What your current code is doing is binding is binding a click event handler every time you blur an element with the class field . This won't work as you want it to.

The best cross-browser way I can think of to do this is to listen for focus events and log which element has received focus, taking action on the previously active element if desired:

$(document).ready(function(){
    var curFocus;

    $(document.body).delegate('*','focus', function(){
        if ((this != curFocus) && // don't bother if this was the previous active element                
            ($(curFocus).is('.field')) && // if it was a .field that was blurred
            ($(this).is('.adress')) // the newly focused field is an .adress
        ) {
            $(curFocus).next('.spacer').children().removeClass("visible"); // take action based on the blurred element
        }

        curFocus = this; // log the newly focussed element for the next event
    });
});

This probably has pretty poor performance. If you aren't worried about cross-browser compatibility, especially for old versions, document.activeElement may be of interest to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM