Affichage des articles dont le libellé est jQuery. Afficher tous les articles

Download document : jQuery Selectors


Download document : jQuery Selectors 





Download document : jQuery Selectors




















CONTENTS INCLUDE:
 What are jQuery Selectors?
 Types of jQuery Selectors
 Basic CSS Selectors
 Custom jQuery Selectors
 Matched Set Methods
 Hot Tips and more...




************






What are jQuery selectors?

jQuery selectors are one of the most important aspects of the jQuery library. These selectors use familiar CSS syntax to allow page authors to quickly and easily identify any set of page elements to operate upon with the jQuery library methods.

Understanding jQuery selectors is the key to using the jQuery library most effectively. This reference card puts the power of jQuery selectors at your very fingertips.

A jQuery statement typically follows the syntax pattern: $(selector).methodName();

The selector is a string expression that identifies the set of DOM elements that will be collected into a matched set to be operated upon by the jQuery methods.

Many of the jQuery operations can also be chained: $(selector).method1().method2().method3(); As an example, let’s say that we want to hide the DOM element with the id value of goAway and to add class name incognito:

$(‘#goAway’).hide().addClass(‘incognito’);

Applying the methods is easy. Constructing the selector expressions is where the cleverness lies.

TYPES OF jQuery selectors

There are three categories of jQuery selectors: Basic CSS selectors, Positional selectors, and Custom jQuery selectors.

The Basic Selectors are known as “find selectors” as they are used to find elements within the DOM. The Positional and Custom Selectors are “filter selectors” as they filter a set of elements (which defaults to the entire set of elements in the DOM).


jQuery Custom Selectors
These selectors are provided by jQuery to allow for commonly used, or just plain handy, selections that were not anticipated by the CSS Specification. Like the Positional Selectors, these selectors filter a base matching set (which we denote with B).

Omitting B is interpreted as the set of all elements. These selectors may be combined; see the examples for some powerful selector combinations.......




















Download document : jQuery Selectors 


0

jQuery Reference / PDF

jQuery Reference / PDF






jQuery Reference / PDF















Targeting elements using jQuery
$("#idName") - targets an id
$(".className") - targets a class
$("htmlTag") - targets an HTML tag

These methods can be mixed and matched. For example, to target all the paragraphs inside a DIV (box) with an ID of "container" you could use:
$("#container p")

jQuery FX

jQuery comes packaged with an array of FX:

• show()
• hide()

The show() and hide() FX work by essentially toggling the value of CSS property display between "block" and "none".

• fadeIn()
• fadeOut()

The fadeIn() and fadeOut() FX work by scaling the CSS opacity property. These two FX can also accept a duration setting inside the brackets. Accepted values are "slow", "fast" or a numerical value measured in milliseconds.

Manipulating CSS
jQuery's power comes from its ability to manipulate CSS properties. All CSS properties can
be accessed and their values set using the following syntax:
$("#idName").css({
backgroundColor: "#FF0000",
fontSize: "15px",
opacity: 0.6
})

Note: if you have multiple CSS properties then a comma is used to separate them. The last item is NOT followed by a comma.

Note: CSS properties such as background-color are camel-cased in jQuery, e.g. backgroundColor.

Note: The opacity property accepts values from 0 to 1, where 0 is fully transparent and 1 is fully opaque.

Animation
jQuery can animate elements (IDs, classes and tags) using the animate() function. For

example:
$("#idName").animate({
left: 200,
top: 300
});

The above example will animate the left and top CSS properties to 200px and 300px respectively.

The animate() function can also accept a duration inserted BEFORE the closing bracket.

For example to set a duration of 6 seconds we would use the following:
$("#idName").animate({
left: 200,
top: 300
}, 6000);

Note: The CSS property values in the above example will move an element to that fixed point. If you just want to move an element 100px from its current position you need to use the following syntax to add 100 onto the current position. Note: This value is written as a string:

$("#idName").animate({
left: "+=100"
});

If the element is currently 200px from the left, then the above example will move it to 300px from the left.

Chaining FX and Animations
jQuery's dot syntax makes it easy to chain FX together. For example:
$("#idName").fadeIn(3000).animate({
left: 400
}, 3000);
In this example, the ID will fade in over 3 se....







Download jQuery Reference / PDF




jQuery Reference / PDF

0

Gratuito PDF: Fundamentos de jQuery

Fundamentos de jQuery 



















1. Bienvenido/a 
1.1. Obtener el Material de Aprendizaje . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2. Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3. Añadir JavaScript a una Página . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4. Depuración del Código JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.5. Ejercicios . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.6. Convenciones Utilizadas en el Libro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.7. Notas de la Traducción . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.8. Material de Referencia . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2. Conceptos Básicos de JavaScript 6
2.1. Introducción . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.2. Sintaxis Básica . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.3. Operadores . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.3.1. Operadores Básicos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.3.2. Operaciones con Números y Cadenas de Caracteres . . . . . . . . . . . . . . . . 7
2.3.3. Operadores Lógicos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.3.4. Operadores de Comparación . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.4. Código Condicional . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.4.1. Elementos Verdaderos y Falsos . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.4.2. Variables Condicionales Utilizando el Operador Ternario . . . . . . . . . . . . . 10
2.4.3. Declaración Switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.5. Bucles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.5.1. Bucles Utilizando For . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.5.2. Bucles Utilizando While . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.5.3. Bucles Utilizando Do-while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.5.4. Break y Continue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1
2.6. Palabras Reservadas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.7. Arreglos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.8. Objetos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.9. Funciones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.9.1. Utilización de Funciones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.9.2. Funciones Anónimas Autoejecutables . . . . . . . . . . . . . . . . . . . . . . . . 18
2.9.3. Funciones como Argumentos . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.10. Determinación del Tipo de Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.11. La palabra clave this . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.12. Alcance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.13. Clausuras . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3. Conceptos Básicos de jQuery 26
3.1. $(document).ready() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.2. Selección de Elementos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.2.1. Comprobar Selecciones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.2.2. Guardar Selecciones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.2.3. Refinamiento y Filtrado de Selecciones . . . . . . . . . . . . . . . . . . . . . . . 30
3.2.4. Selección de Elementos de un Formulario . . . . . . . . . . . . . . . . . . . . . 30
3.3. Trabajar con Selecciones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.3.1. Encadenamiento . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.3.2. Obtenedores (Getters) & Establecedores (Setters) . . . . . . . . . . . . . . . . . 32
3.4. CSS, Estilos, & Dimensiones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
3.4.1. Utilizar Clases para Aplicar Estilos CSS . . . . . . . . . . . . . . . . . . . . . . 33
3.4.2. Dimensiones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
3.5. Atributos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
3.6. Recorrer el DOM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.7. Manipulación de Elementos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3.7.1. Obtener y Establecer Información en Elementos . . . . . . . . . . . . . . . . . . 35
3.7.2. Mover, Copiar y Remover Elementos . . . . . . . . . . . . . . . . . . . . . . . . 36
3.7.3. Crear Nuevos Elementos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
3.7.4. Manipulación de Atributos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
3.8. Ejercicios . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
3.8.1. Selecciones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
3.8.2. Recorrer el DOM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
3.8.3. Manipulación . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
2
4. El núcleo de jQuery 41
4.1. $ vs $() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
4.2. Métodos Utilitarios . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
4.3. Comprobación de Tipos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
4.4. El Método Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
4.5. Detección de Navegadores y Características . . . . . . . . . . . . . . . . . . . . . . . . 44
4.6. Evitar Conflictos con Otras Bibliotecas JavaScript . . . . . . . . . . . . . . . . . . . . 44
5. Eventos 46
5.1. Introducción . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
5.2. Vincular Eventos a Elementos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
5.2.1. Vincular Eventos para Ejecutar una vez . . . . . . . . . . . . . . . . . . . . . . 47
5.2.2. Desvincular Eventos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
5.2.3. Espacios de Nombres para Eventos . . . . . . . . . . . . . . . . . . . . . . . . . 47
5.2.4. Vinculación de Múltiples Eventos . . . . . . . . . . . . . . . . . . . . . . . . . . 48
5.3. El Objeto del Evento . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
5.4. Ejecución automática de Controladores de Eventos . . . . . . . . . . . . . . . . . . . . 49
5.5. Incrementar el Rendimiento con la Delegación de Eventos . . . . . . . . . . . . . . . . 49
5.5.1. Desvincular Eventos Delegados . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
5.6. Funciones Auxiliares de Eventos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
5.6.1. $.fn.hover . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
5.6.2. $.fn.toggle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
5.7. Ejercicios . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
5.7.1. Crear una “Sugerencia” para una Caja de Ingreso de Texto . . . . . . . . . . . 51
5.7.2. Añadir una Navegación por Pestañas . . . . . . . . . . . . . . . . . . . . . . . . 51
6. Efectos 53
6.1. Introducción . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
6.2. Efectos Incorporados en la Biblioteca . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
6.2.1. Cambiar la Duración de los Efectos . . . . . . . . . . . . . . . . . . . . . . . . . 54
6.2.2. Realizar una Acción Cuando un Efecto fue Ejecutado . . . . . . . . . . . . . . 54
6.3. Efectos Personalizados con $.fn.animate . . . . . . . . . . . . . . . . . . . . . . . . . 55
6.3.1. Easing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
6.4. Control de los Efectos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
6.5. Ejercicios . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
6.5.1. Mostrar Texto Oculto . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
6.5.2. Crear un Menú Desplegable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
6.5.3. Crear un Slideshow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
3
7. Ajax 58
7.1. Introducción . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
7.2. Conceptos Clave . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
7.2.1. GET vs. POST . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
7.2.2. Tipos de Datos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
7.2.3. Asincronismo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
7.2.4. Políticas de Mismo Origen y JSONP . . . . . . . . . . . . . . . . . . . . . . . . 60
7.2.5. Ajax y Firebug . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
7.3. Métodos Ajax de jQuery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
7.3.1. $.ajax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
7.3.2. Métodos Convenientes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
7.3.3. $.fn.load . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
7.4. Ajax y Formularios . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
7.5. Trabajar con JSONP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
7.6. Eventos Ajax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
7.7. Ejercicios . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
7.7.1. Cargar Contenido Externo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
7.7.2. Cargar Contenido Utilizando JSON . . . . . . . . . . . . . . . . . . . . . . . . 66
8. Extensiones 67
8.1. ¿Qué es una Extensión? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
8.2. Crear una Extensión Básica . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
8.3. Encontrar y Evaluar Extensiones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
8.4. Escribir Extensiones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
8.5. Escribir Extensiones con Mantenimiento de Estado Utilizando Widget Factory de
jQuery UI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
8.5.1. Añadir Métodos a un Widget . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
8.5.2. Trabajar con las Opciones del Widget . . . . . . . . . . . . . . . . . . . . . . . 74
8.5.3. Añadir Funciones de Devolución de Llamada . . . . . . . . . . . . . . . . . . . 75
8.5.4. Limpieza . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
8.5.5. Conclusión . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
8.6. Ejercicios . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
8.6.1. Realizar una Tabla Ordenable . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
8.6.2. Escribir una Extensión Para Cambiar el Color de Fondo en Tablas . . . . . . . 78
4
9. Mejores Prácticas para Aumentar el Rendimiento 79
9.1. Guardar la Longitud en Bucles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
9.2. Añadir Nuevo Contenido por Fuera de un Bucle . . . . . . . . . . . . . . . . . . . . . . 79
9.3. No Repetirse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
9.4. Cuidado con las Funciones Anónimas . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
9.5. Optimización de Selectores . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
9.5.1. Selectores basados en ID . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
9.5.2. Especificidad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
9.5.3. Evitar el Selector Universal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
9.6. Utilizar la Delegación de Eventos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
9.7. Separar Elementos para Trabajar con Ellos . . . . . . . . . . . . . . . . . . . . . . . . 83
9.8. Utilizar Estilos en Cascada para Cambios de CSS en Varios Elementos . . . . . . . . . 83
9.9. Utilizar $.data en Lugar de $.fn.data . . . . . . . . . . . . . . . . . . . . . . . . . . 83
9.10. No Actuar en Elementos no Existentes . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
9.11. Definición de Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
9.12. Condicionales . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
9.13. No Tratar a jQuery como si fuera una Caja Negra . . . . . . . . . . . . . . . . . . . . 85
10.Organización del Código 86
10.1. Introducción . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
10.1.1. Conceptos Clave . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
10.2. Encapsulación . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
10.2.1. El Objeto Literal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
10.2.2. El Patrón Modular . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
10.3. Gestión de Dependencias . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
10.3.1. Obtener RequireJS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
10.3.2. Utilizar RequireJS con jQuery . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
10.3.3. Crear Módulos Reusables con RequireJS . . . . . . . . . . . . . . . . . . . . . . 93
10.3.4. Optimizar el Código con las Herramientas de RequireJS . . . . . . . . . . . . . 94
10.4. Ejercicios . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
10.4.1. Crear un Módulo Portlet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
11.Eventos Personalizados 97
11.1. Introducción a los Eventos Personalizados . . . . . . . . . . . . . . . . . . . . . . . . . 97
11.1.1. Un Ejemplo de Aplicación . . . . . .

















Fundamentos de jQuery 



0

jQuery tutorial : The Basics


jQuery  : The Basics


















This is a basic tutorial, designed to help you get started using jQuery. If you don't have a test page setup yet, start by creating a new HTML page with the following contents:
 
   
    Demo
 
 
    jQuery
   
   
 
Edit the src attribute in the script tag to point to your copy of jquery.js. For example, if jquery.js is in the same directory as your HTML file, you can use:


Launching Code on Document Ready
The first thing that most Javascript programmers end up doing is adding some code to their program, similar to this:
 window.onload = function(){ alert("welcome"); }
Inside of which is the code that you want to run right when the page is loaded. Problematically, however, the Javascript code isn't run until all images are finished downloading (this includes banner ads). The reason for using window.onload in the first place is that the HTML 'document' isn't finished loading yet, when you first try to run your code.
To circumvent both problems, jQuery has a simple statement that checks the document and waits until it's ready to be manipulated, known as the ready event:
 $(document).ready(function(){
   // Your code here
 });
Inside the ready event, add a click handler to the link:
 $(document).ready(function(){
   $("a").click(function(event){
     alert("Thanks for visiting!");
   });
 });
Save your HTML file and reload the test page in your browser. Clicking the link on the page should make a browser's alert pop-up, before leaving to go to the main jQuery page.
For click and most other events, you can prevent the default behaviour - here, following the link to jquery.com - by calling event.preventDefault() in the event handler:
 $(document).ready(function(){
   $("a").click(function(event){
     alert("As you can see, the link no longer took you to jquery.com");
     event.preventDefault();
   });
 });

Complete Example
The following is an example of what the complete HTML file might look like if you were to use the script in your own file. Note that it links to Google's CDN to load the jQuery core file. Also, while the custom script is included in the , it is generally preferable to place it in a separate file and refer that file with the script element's src attribute



  
   jQuery demo


   http://jquery.com/">jQuery
  
  



Adding and Removing an HTML Class
Important: The remaining jQuery examples will need to be placed inside the ready event so that they are executed when the document is ready to be worked on. See Launching Code on Document Ready above for details.
Another common task is adding (or removing) a class.
First, add some style information into the of your document, like this:

Next, add the addClass call to your script:
  $("a").addClass("test");
All your a elements will now be bold.
To remove the class, use removeClass
 $("a").removeClass("test");
§  (HTML allows multiple classes to be added to an element.)
[edit]
Special Effects
In jQuery, a couple of handy effects are provided, to really make your web site stand out. To put this to the test, change the click that you added earlier to this:
 $("a").click(function(event){
   event.preventDefault();
   $(this).hide("slow");
 });
Now, if you click any link, it should make itself slowly disappear.

CALLBACK AND FUNCTIONS
A callback is a function that is passed as an argument to another function and is executed after its parent function has completed. The special thing about a callback is that functions that appear after the "parent" can execute before the callback executes. Another important thing to know is how to properly pass the callback. This is where I have often forgotten the proper syntax.

Callback without arguments
For a callback with no arguments you pass it like this:
 $.get('myhtmlpage.html', myCallBack);
Note that the second parameter here is simply the function name (but not as a string and without parentheses). Functions in Javascript are 'First class citizens' and so can be passed around like variable references and executed at a later time.

Callback with arguments
"What do you do if you have arguments that you want to pass?", you might ask yourself.

Wrong
The Wrong Way (will not work!)
 $.get('myhtmlpage.html', myCallBack(param1, param2));

This will not work because it calls
myCallBack(param1, param2)
and then passes the return value as the second parameter to $.get()

Right
The problem with the above example is that myCallBack(param1, param2) is evaluated before being passed as a function. Javascript and by extension jQuery expects a function pointer in cases like these. I.E. setTimeout function.....


















jQuery  : The Basics
0

Download E-book : Learning from jQuery


Download E-book : Learning from jQuery / PDF









Download E-book : Learning from jQuery / PDF
















1. Event Handling.
Listening for Events 1
Events in jQuery 1
Events in JavaScript 2
Events in Internet Explorer 8 2
Writing a Wrapper Function 3
Adding Event Handlers to Multiple Elements 5
Event Propagation 7
Internet Explorer’s .attachEvent 10
Triggering Events 11
Triggering Events in Internet Explorer 8 13
Writing a Wrapper Function to Trigger Events 13
Removing Event Handlers 14
Removing Event Handlers in Internet Explorer 8 15
Writing a Wrapper Function to Remove Events 15
Adding a “Once Only” Event Listener 16


2. Constructors and Prototypes 
Constructors 19
Method Chaining 20
Constructor, Not Function 21
Prototypes 22
.hasOwnProperty 24
Editing the Prototype of Existing Objects 24
Summary 25

3. DOM Traversal and Manipulation

Selecting an Element 27
Selecting Elements with a CSS Selector 28
Selecting Children 29
Selecting the Next Element 30
Creating an Element 31
Modifying an Existing Element 31
Cycling Through Elements 33
Moving and Copying Elements 33


4. AJAX. 
Sending an AJAX Request 35
Debugging 36
Debugging Sent AJAX Requests 37
Sending POST Requests in JavaScript 37
Writing a Wrapper Function 38
A Simple Application of AJAX 39
Designing a Site with AJAX 41


5. JavaScript Conventions.
Writing JavaScript 43
Comments 43
Coding Standards 46
Literals Notation 50
Object Literals 50
Other Literals 51
Optimizations 51
Algorithms 52
Caching Variables 52
parseInt 53
Loops 53
Minimize Repeated Expressions 54
Functions 54
Declarations Versus Expressions 54
Function Callbacks 55
If Invoking Self-Defining Functions 56
Code Reuse 58
Common Antipatterns 58
Using eval 59
with 59
document.write

Common Design Patterns 60
The Singleton Pattern 61
The Factory Pattern 62
The Iterator Pattern 63
The Facade Pattern 64















Download E-book : Learning from jQuery / PDF


0

Tags