Visualizzazione dei risultati da 1 a 4 su 4
  1. #1
    Utente di HTML.it L'avatar di m4rko80
    Registrato dal
    Aug 2008
    residenza
    Milano
    Messaggi
    2,655

    Jquery .live() vs. .on()

    Ciao a tutti, leggendo la documentazione ho letto che .on() nella versione 1.7 di jquery sostituisce live() che dovrebbe essere quindi deprecato.

    Forse sbaglio io a scrivere qualcosa ma dopo un load del nuovo html l'evento non viene piu' preso.

    Con live usavo

    jQuery(ajax_search_class).live('keyup', function(){ ....

    Con on uso:
    jQuery(ajax_search_class).on('keyup', function(){ ..
    funziona ma sono al primo load dell'html

    Ho letto che bisognerebbe passare un'altra ancora sempre statica per poter usarlo come il live

    jQuery(ajax_search_class).on('keyup', ,'body', function(){ ... ad esempio non mi funziona.

    Dove sbaglio per usarlo come il live di prima?

    grazie

  2. #2
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649
    dalla descrizione di .on()
    Event handlers are bound only to the currently selected elements; they must exist on the page at the time your code makes the call to .on(). To ensure the elements are present and can be selected, perform event binding inside a document ready handler for elements that are in the HTML markup on the page. If new HTML is being injected into the page, select the elements and attach event handlers after the new HTML is placed into the page. Or, use delegated events to attach an event handler, as described next.

    Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time.
    By picking an element that is guaranteed to be present at the time the delegated event handler is attached, you can use delegated events to avoid the need to frequently attach and remove event handlers. This element could be the container element of a view in a Model-View-Controller design, for example, or document if the event handler wants to monitor all bubbling events in the document. The document element is available in the head of the document before loading any other HTML, so it is safe to attach events there without waiting for the document to be ready.
    quindi il tuo .on non ha piu' come selettore ajax_search_class, ma un suo contenitore sicuramente presente al momento
    $(selettoreContenitore).on('keyup',ajax_search_cla ss, function(){...})

    ciao

  3. #3
    Utente di HTML.it L'avatar di m4rko80
    Registrato dal
    Aug 2008
    residenza
    Milano
    Messaggi
    2,655
    Ciao e grazie per la risposta.
    In pratica la situazione e' un po diversa.
    Il selettore c'e' sempre all'inizio ma il tutto sta all'interno di un widget(wordpress)
    Se non hai mai usato wordpress per spiegarti in breve, tutto il blocco html puo' essere ricaricato premendo "salva". La pagina ovviamente non ricarica ma solo tutto quel blocco selettore compreso.
    Domattina provo come hai mostrato.
    Intanto grazie

  4. #4
    Utente di HTML.it L'avatar di m4rko80
    Registrato dal
    Aug 2008
    residenza
    Milano
    Messaggi
    2,655
    Ok, usando un elemento fisso ora funziona.
    grazie

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.