jQuery 1.9: le novità ed i cambiamenti

jQuery 1.9: le novità ed i cambiamenti

Questo articolo offre una panoramica dei principali cambiamenti avvenuti in jQuery 1.9. Per avere una visione completa delle modifiche si consulti la documentazione ufficiale di jQuery.

Rimosse le due funzioni di callback da toggle()

toggle() non può più essere usato con le due funzioni di callback.

.toggle(function, function, ... ) removed

This is the "click an element to run the specified functions" signature of .toggle(). It should not be confused with the "change the visibility of an element" of .toggle() which is not deprecated. The former is being removed to reduce confusion and improve the potential for modularity in the library. The jQuery Migrate plugin can be used to restore the functionality.

Rimosso $.browser()

Questa funzionalità è stata rimossa in jQuery 1.9.

jQuery.browser() removed

The jQuery.browser() method has been deprecated since jQuery 1.3 and is removed in 1.9. If needed, it is available as part of the jQuery Migrate plugin. We recommend using feature detection with a library such as Modernizr.

Rimosso .live()

Questo metodo/evento è stato sostituito da on().

.live() removed

The .live() method has been deprecated since jQuery 1.7 and has been removed in 1.9. We recommend upgrading code to use the .on() method instead. To exactly match $("a.foo").live("click", fn), for example, you can write $(document).on("click", "a.foo", fn). For more information, see the .on() documentation. In the meantime, the jQuery Migrate plugin can be used to restore the .live() functionality.

Rimosso .die()

Questo metodo/evento è stato sostituito da off().

.die() removed

The .die() method has been deprecated since jQuery 1.7 and has been removed in 1.9. We recommend upgrading code to use the .off() method instead. To exactly match $("a.foo").die("click"), for example, you can write $(document).off("click", "a.foo"). For more information, see the .off() documentation. In the meantime, the jQuery Migrate plugin can be used to restore the .die() functionality.

Rimosso .sub()

jQuery.sub() removed

The jQuery.sub() method has been moved to the jQuery Migrate plugin. The number of use cases where it proved valuable were not enough to justify keeping it in core. The jQuery Migrate plugin adds back this functionality.

Modificato .add()

Questo metodo ora rispetta l'ordine interno del documento.

add()

The .add() method is always supposed to return its results in document order. Prior to 1.9, .add() would not sort nodes in document order if either the context or the input set started with a disconnected node (not in a document). Now, nodes are always returned in document order and disconnected nodes are placed at the end of the set.

.addBack() sostituisce .andSelf()

.addBack( selector ) replaces .andSelf()

As of jQuery 1.8, the .andSelf() method was deprecated in favor of the .addBack() method, which we feel is a better name for what this method does--"add back" the previous set of results. The new method accepts an optional selector that can be used to filter the previous set before adding it to the current set. So, $("section, aside").children("ul").addBack("aside") results in a set that includes all aside nodes plus the ul children of both section and aside nodes, in document order. Although the .andSelf() method still works in 1.9, we recommend switching names as soon as possible. The jQuery Migrate plugin will warn about the use of .andSelf().

.after(), .before() e .replaceWith()

Questi tre metodi ora restituiscono sempre il set originale di elementi e se l'elemento non ha un genitore non vi è alcun effetto.

.after(), .before(), and .replaceWith() with disconnected nodes

Prior to 1.9, .after(), .before(), and .replaceWith() would attempt to add or change nodes in the current jQuery set if the first node in the set was not connected to a document, and in those cases return a new jQuery set rather than the original set. This created several inconsistencies and outright bugs--the method might or might not return a new result depending on its arguments! As of 1.9, these methods always return the original unmodified set and attempting to use .after(), .before(), or .replaceWith() on a node without a parent has no effect--that is, neither the set or the nodes it contains are changed.

.appendTo(), .insertBefore(), .insertAfter(), e .replaceAll()

Questi metodi ora restituiscono sempre un nuovo set.

.appendTo, .insertBefore, .insertAfter, and .replaceAll

As of 1.9, these methods always return a new set, making them consistently usable with chaining and the .end() method. Prior to 1.9, they would return the old set only if there was a single target element. Note that these methods have always returned the aggregate set of all elements appended to the target elements. If no elements are selected by the target selector (e.g., $(elements).appendTo("#not_found")) the resulting set will be empty.

Gli eventi globali AJAX vanno legati a document

Ora gli eventi globali AJAX vanno definiti in questo modo:


$(document).ajaxStart(function(){ 
	$("#status").text("Ajax started"); 
});

AJAX events should be attached to document

As of jQuery 1.9, the global AJAX events (ajaxStart, ajaxStop, ajaxSend, ajaxComplete, ajaxError, and ajaxSuccess) are only triggered on the document element.

Stringhe HTML nel wrapper $()

Ora solo le stringhe ben formate vengono considerate come validi elementi HTML. Tutte le altre stringhe vengono trattate come selettori.

jQuery(htmlString) versus jQuery(selectorString)

Prior to 1.9, a string would be considered to be an HTML string if it had HTML tags anywhere within the string. This has the potential to cause inadvertent execution of code and reject valid selector strings. As of 1.9, a string is only considered to be HTML if it starts with a less-than ("<") character. The Migrate plugin can be used to restore the pre-1.9 behavior.

If a string is known to be HTML but may start with arbitrary text that is not an HTML tag, pass it to jQuery.parseHTML() which will return an array of DOM nodes representing the markup. A jQuery collection can be created from this, for example: $($.parseHTML(htmlString)). This would be considered best practice when processing HTML templates for example. Simple uses of literal strings such as $("<p>Testing</p>").appendTo("body") are unaffected by this change.

Bottom line: HTML strings passed to jQuery() that start with something other than a less-than character will be interpreted as a selector. Since the string usually cannot be interpreted as a selector, the most likely result will be an "invalid selector syntax" error thrown by the Sizzle selector engine. Use jQuery.parseHTML() to parse arbitrary HTML.

When the jQuery Migrate plugin is used, it will use the old rules for determining if the string passed to $() "looks like HTML".

Torna su