簡體   English   中英

jQuery樹遍歷問題

[英]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);
        });
});

每當我模糊分類為.field的字段時,我都會嘗試刪除“可見的類”,除非單擊帶有.adress類的元素。

字段模糊和地址單擊可以正常工作(我對警報感到厭倦),但不能刪除班級,有人知道為什么嗎?

如果刪除了not(“。adress”)函數,則刪除類將起作用! 像這樣:

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

您在//$(this之后有一個額外的引號。您無需引用this變量。

此外,您的*選擇器可以簡化為$(':not(.address)')

您能提供一個HTML示例嗎?

每當我模糊分類為.field的字段時,我都會嘗試刪除“可見的類”,除非單擊帶有.adress類的元素。

這很棘手。 您當前的代碼正在執行的操作是,每次使用class field模糊元素時,都將綁定click事件處理程序。 這將無法正常工作。

我想到的最好的跨瀏覽器方式是偵聽focus事件並記錄哪個元素已獲得焦點,並根據需要對先前處於活動狀態的元素采取措施:

$(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
    });
});

這可能性能很差。 如果您不擔心跨瀏覽器的兼容性,尤其是對於舊版本,則document.activeElement可能會讓您感興趣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM