jQuery constructor de objetos dobles - por ejemplo, $($(this))
Frecuentes
Visto 349 veces
4
Suponga que tiene el siguiente código:
function name() {
$(this).css('background', 'red');
}
$('selector1').click(name);
$('selector2').click(function () {
name.call($(this).parent());
});
Now, when the function is called by clicking on 'selector1' this
is an HTML object and $(this)
a jQuery object, but if the function is called by clicking on 'selector2' this
is already a jQuery object so what is $(this)
?
Sé que podría hacer algo como name.call($(this).parent()[0]);
to get an HTML object, but my question is what happens when you do something like $($(this))
or $($('selector'))
? What is the result of that and, most impotently, is there any harm in using such a construct?
1 Respuestas
6
$(this)
cuando this
is already a jQuery object creates a copy of the jQuery object.
Desde el documentos de jQuery:
Clonación de objetos jQuery
When a jQuery object is passed to the
$()
function, a clone of the object is created. This new jQuery object references the same DOM elements as the initial one.
Respondido el 20 de junio de 20 a las 10:06
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas javascript jquery or haz tu propia pregunta.
A much more popular duplicate: stackoverflow.com/q/21671105/2157640 - Palec