¿Alguna forma de convertir esto en una función jQuery?

I have tried to wrap this in a function and cannot get it to work. I am wondering if it is because it is appending something to the body? But here is the script. Any help is greatly appreciated.

$('.showIt').click(function(){
$('body').append("<div class='show'></div>");
});

I would like to put that into a function i can call, but i cant seem to get it to work, This is what i have tried and it fails to work for me.

function create(){
$('body').append("<div class='show'></div>");
});
$('.showIt').click(function(){
create():
});

¿Puede ayudarme alguien, por favor?

preguntado el 09 de enero de 11 a las 03:01

3 Respuestas

Tienes un extra );.
Desaste de eso.

Your first function is written as function create(){ ... });

Respondido el 09 de enero de 11 a las 06:01

Esto agregará un div.show to body when you'll click on a link/element with class .showIt

function create(){
$('body').append("<div class='show'></div>");
}

$('.showIt').click(function(){
create();
});

In your code you had a });cuando deberías tener } y : cuando ; should be Is that what you're looking for?

Respondido el 09 de enero de 11 a las 06:01

function create(){
    $('body').append("<div class='show'></div>");
}

$('.showIt').click(function(){
    create();
});

after cleaning up your code a bit I don't know why you can't do this.

you did have an extra ); after closing your create() function though.

Respondido el 09 de enero de 11 a las 06:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.