Establecer un margen basado en una variable agregada a un número

I'm trying to have the margin-bottom of an element being altered to the height of the next() element plus 12px. I tried the following, but this doesn't work.

$("div.entry-content").css("margin-bottom", $(this).next().outerHeight() + 12 + "px");

¿Alguna idea de lo que sale mal?

preguntado el 25 de agosto de 12 a las 15:08

1 Respuestas

Prueba esto:

$("div.entry-content").css("margin-bottom", function(){
    return $(this).next().outerHeight() + 12 + "px"
})

Respondido 25 ago 12, 15:08

@snuffn Yes, I had forgotten to write the return. - Carnero

Works like a charm, thanks! Do you mind explaining why a function is needed? - bram vanroy

@BramVanroy Because this in your code doesn't refer to your desired element, this in the context of the function refers to the selected element, you can read about that in api.jquery.com/css/#css2 - Carnero

Thanks a lot! Will read it through when I have sufficient time. I bumped into a similar problem. If you have time, please look into it: stackoverflow.com/questions/12123267/… - bram vanroy

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