I’m pretty newbie with jQuery mobile (and JS :)), excuse if I’m saying something that looks too easy. I’ve searched through the Internet for two days now and haven’t find a solution.
I want to know if there’s some function that does the same that $(#someList).listview(‘refresh’) but for div tags. I’ve got two div tags and when I dinamycally change its contents they loose the styling.
Code:
function muestraFicha() {
var categoriaFicha = "Ciencia / Matemáticas";
var preguntaFicha = "Si un tren eléctrico sale de Valencia dirección Barcelona a 60Km/h con viento norte de 30Km/h ¿Hacia qué dirección saldrá el viento de su chimenea?";
var respuestaFicha = "En ninguna dirección, es un tren eléctrico y por lo tanto no despide humo";
var divRespuesta = "<div data-role='collapsible' data-theme='b' data-content-theme='c'>"
$("#fichaRespuesta").empty(); //vaciamos la ficha
$("#fichaRespuesta").append("<h4> Respuesta </h4>");
$("#fichaRespuesta").append("<p>" + respuestaFicha + "</p>");
$("#fichaPregunta").empty();
$("#fichaPregunta").append("<h3>" + categoriaFicha + "</h3>");
$("#fichaPregunta").append("<p>" + preguntaFicha + "</p>");
}
So, at the end of the function I need something a the .listview(‘refresh’) or the html is only styled the first time the function is called.
The divs that lose style are fichaRespuesta and fichaPregunta.
Thank you.
If you want to have jQuery Mobile style any new DOM element, you can use the
.trigger('create');function:Link to the above information: http://jquerymobile.com/blog/2011/08/03/jquery-mobile-beta-2-released/
Also I notice you are using the same selectors consecutively, you can greatly improve the performance of your code by chaining together the calls to selectors, like so:
Change:
To:
Notice I removed one of the
.append()calls since it creates extra overhead to call it twice in a row; instead I put the HTML for both.append()calls in a single call.If you really want to get into making your code performance improve, cache the selectors so they only have to be looked-up once, like so: