2010-07-30 10 views
10

¿Cómo puedo pasar el $ esto (la palabra clave auto) a una función en Jquery

$(document).ready(function() { 

    $('.nav a').click(function() { 
     var direction = $(this).attr("name"); 
     submit_form($(this)) 
    }); 

    function submit_form(this) 
    { 
     // do some stuff with 'this' 
    }  
}); 
+4

Ya lo hace correctamente. Simplemente cambie el nombre de la variable en 'function submit_form (this) {}' a otra cosa como 'function submit_form (element) {}'. –

+1

Utilice javascript de la manera en que estaba destinado a ser utilizado (no es necesario pasar el contexto como parámetro): 'submit_form.call (this)', 'submit_form.apply (this)', o incluso use jQuery si eso es lo único usted entiende: 'jQuery.proxy (submit_form, this)' –

Respuesta

16

envolviéndolo en $() lo convierte en un jQuery objeto. Desea hacer algo como

submit_form(this); 

function submit_form(obj) 
{ 
    // Work with `obj` as "this" 
    // Or wrap it in $() to work with it as jQuery(this) 
} 
+1

$ (¡punto importante!) –

1

Prueba esto:

$(document).ready(function() 
    { 
     $('.nav a').click(function() { 
      var direction = $(this).attr("name"); 
      submit_form(this); 
     }); 

     function submit_form(myObject) 
     { 
      // do some stuff with 'myObject' 


     }   

    }); 
0

¿Solo pasarlo como una variable normal?

function submit_form(context) 
{ 
    context.html("Boo!"); 

} 

/// Then when you call it: 
submit_form($(this)); 
5

El this palabra clave no se puede establecer en JavaScript. Es generado automáticamente por JavaScript y "siempre se refiere al" propietario "de la función que estamos ejecutando, o más bien, al objeto que una función es un método de".
http://www.quirksmode.org/js/this.html

Usted tiene un problema en su código (tratando de nombrar el parámetro de la función submit_form() para esto). Sin embargo, la forma en que se distribuye su código no deja en claro si tiene la intención de pasar el ancla hecha clic como un objeto jQuery o como el nodo DOM para el anclaje.

$(document).ready(function() { 
    $('.nav a').click(function() { 
     $anchor = $(this);      // Capture the jQuery-wrapped anchor for re-use; 'this' is an anchor because it matched $('.nav a') 
     var direction = $anchor.attr("name"); // $variable_name is a standard pattern in jQuery to indicate variables that are jQuery-wrapped instead of DOM objects 

     submit_form_jquery($anchor);   // Providing versions of submit_form function for passing either jQuery-wrapped object or DOM object 
     submit_form_dom(this);     // Pick the one you prefer and use it 
    }); 

    function submit_form_jquery($my_anchor) { // Function with well-named parameter expecting a jQuery-wrapped object 
     // do some stuff with '$my_anchor' 
     // $my_anchor here is assumed to be a jQuery-wrapped object 
    } 

    function submit_form_dom(anchor) {   // Function named expecting a DOM element, not a jQuery-wrapped element 
     // do some stuff with 'anchor' 
     // anchor here is assumed to be a DOM element, NOT wrapped in a jQuery object 
    } 
}); 

En una nota en su mayoría sin relación, es probable que desee que sea return false; o utilizar event.preventDefault() para mantener la página de detrás de las href en el anclaje que se hizo clic. Puede hacerlo de la siguiente manera:

$(document).ready(function() { 
    $('.nav a').click(function(event) { 
     event.preventDefault(); 
     // And now do what you want the click to do 
    }); 
}); 
1

Esto es lo que funcionó para mí.

$(document).ready(function() 
{ 
    $('.nav a').click(function() { 
     submit_form(this) 
    }); 

    function submit_form(thisObject) 
    { 
     var direction = $(thisObject).attr("name"); 
    }  
}); 
1

Sí, este se puede ajustar fácilmente. Ver Function.call() y Function.apply().

Cuestiones relacionadas