2011-12-28 17 views

risposta

20

È possibile indirizzare classi specifiche che riguardano i widget specifici, resettare le loro classi, e quindi aggiungere la classe a tema della tua scelta:

//set your new theme letter 
    var theme = 'e'; 

    //reset all the buttons widgets 
    $.mobile.activePage.find('.ui-btn') 
         .removeClass('ui-btn-up-a ui-btn-up-b ui-btn-up-c ui-btn-up-d ui-btn-up-e ui-btn-hover-a ui-btn-hover-b ui-btn-hover-c ui-btn-hover-d ui-btn-hover-e') 
         .addClass('ui-btn-up-' + theme) 
         .attr('data-theme', theme); 

    //reset the header/footer widgets 
    $.mobile.activePage.find('.ui-header, .ui-footer') 
         .removeClass('ui-bar-a ui-bar-b ui-bar-c ui-bar-d ui-bar-e') 
         .addClass('ui-bar-' + theme) 
         .attr('data-theme', theme); 

    //reset the page widget 
    $.mobile.activePage.removeClass('ui-body-a ui-body-b ui-body-c ui-body-d ui-body-e') 
         .addClass('ui-body-' + theme) 
         .attr('data-theme', theme); 

http://jsfiddle.net/VNXb2/1/

Questo non è affatto un codice completamente funzionale snippet, dovrai trovare tutti gli altri widget che desideri aggiornare quando cambi il tema e aggiungili al codice sopra. Puoi trovare le classi che ogni widget ha facilmente usando FireBug o un'altra Developer Console.

UPDATE

Quando si prende in considerazione le data-role="list-divider elementi Questo diventa un po 'difficile:

var theme = 'c'; 

//the only difference between this block of code and the same code above is that it doesn't target list-dividers by calling: `.not('.ui-li-divider')` 
$.mobile.activePage.find('.ui-btn').not('.ui-li-divider') 
        .removeClass('ui-btn-up-a ui-btn-up-b ui-btn-up-c ui-btn-up-d ui-btn-up-e ui-btn-hover-a ui-btn-hover-b ui-btn-hover-c ui-btn-hover-d ui-btn-hover-e') 
        .addClass('ui-btn-up-' + theme) 
        .attr('data-theme', theme); 

//target the list divider elements, then iterate through them to check if they have a theme set, if a theme is set then do nothing, otherwise change its theme to `b` (this is the jQuery Mobile default for list-dividers) 
$.mobile.activePage.find('.ui-li-divider').each(function (index, obj) { 
    if ($(this).parent().attr('data-divider-theme') == 'undefined') { 
     $(this).removeClass('ui-bar-a ui-bar-b ui-bar-c ui-bar-d ui-bar-e') 
       .addClass('ui-bar-b') 
       .attr('data-theme', 'b'); 
    } 
}) 

/*The rest of this code example is the same as the above example*/ 

Ecco una demo: http://jsfiddle.net/VNXb2/7/

+0

I m utilizzando list-divisore nella mia applicazione staticamente sta cambiando, ma dinamicamente non cambia che è solo problema. –

+0

@kanna Ho aggiornato la mia risposta, 'data-role =" list-divider "' s richiede un po 'di logica per farli bene, ma penso che la mia risposta sia piuttosto completa: http://jsfiddle.net/VNXb2/7/ – Jasper

+0

@kanna Un'altra idea, se si modifica il tema di un widget prima che venga inizializzato, verrà inizializzato correttamente nel tema desiderato. Se esegui il binding all'evento 'pageInit', i widget non devono essere inizializzati e puoi modificare il tema della pagina o qualsiasi elemento all'interno della pagina. – Jasper

9

La risposta di cui sopra mi ha aiutato molto, L'ho modificato un po 'per il mio bisogno, visto che sto usando themeroller e mi aspetto di avere più di 20 temi. Ecco quello che ho fatto

function updateTheme(newTheme) { 
//alert("In refresh"); 
var rmbtnClasses = ''; 
var rmhfClasses = ''; 
var rmbdClassess = ''; 
var arr = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" ]; 

$.each(arr,function(index, value){ 
    rmbtnClasses = rmbtnClasses + " ui-btn-up-"+value + " ui-btn-hover-"+value; 
    rmhfClasses = rmhfClasses + " ui-bar-"+value; 
    rmbdClassess = rmbdClassess + " ui-body-"+value; 
}); 

// reset all the buttons widgets 
$.mobile.activePage.find('.ui-btn').not('.ui-li-divider').removeClass(rmbtnClasses).addClass('ui-btn-up-' + newTheme).attr('data-theme', newTheme); 

// reset the header/footer widgets 
$.mobile.activePage.find('.ui-header, .ui-footer').removeClass(rmhfClasses).addClass('ui-bar-' + newTheme).attr('data-theme', newTheme); 

// reset the page widget 
$.mobile.activePage.removeClass(rmbdClassess).addClass('ui-body-' + newTheme).attr('data-theme', newTheme); 

// target the list divider elements, then iterate through them and 
// change its theme (this is the jQuery Mobile default for 
// list-dividers) 
$.mobile.activePage.find('.ui-li-divider').each(function(index, obj) { 
$(this).removeClass(rmhfClasses).addClass('ui-bar-' + newTheme).attr('data-theme',newTheme); 

}) 

Ora, quando ottengo il nuovo tema dal server tramite JSON mi basta chiamare questo metodo con il nuovo tema come param.

saluti Rajesh J

+0

Come posso cambiare tema per ? Che classe devo aggiungere qui? – Prasoon

0

risposta di Rajesh mi ha aiutato molto ... Ma Rajesh, ti sei perso una classe importante ---- 'ui-page-tema-*', così ho modificato la tua risposta e ora è perfetto per jQuery mobile 1.4.5 (di nuovo) ...

var updateTheme = function(newTheme) { 
    var rmbtnClasses = ''; 
    var rmhfClasses = ''; 
    var rmbdClassess = ''; 
    var arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's']; // I had themes from a to s 

    $.each(arr, function(index, value) { 
     rmbtnClasses = rmbtnClasses + ' ui-btn-up-' + value + ' ui-btn-hover-' + value; 
     rmhfClasses = rmhfClasses + ' ui-bar-' + value; 
     rmbdClassess = rmbdClassess + ' ui-body-' + value + ' ui-page-theme-'+ value; 
    }); 

    // reset all the buttons widgets 
    $.mobile.activePage.find('.ui-btn').not('.ui-li-divider').removeClass(rmbtnClasses).addClass('ui-btn-up-' + newTheme).attr('data-theme', newTheme); 

    // reset the header/footer widgets 
    $.mobile.activePage.find('.ui-header, .ui-footer').removeClass(rmhfClasses).addClass('ui-bar-' + newTheme).attr('data-theme', newTheme); 

    // reset the page widget 
    $.mobile.activePage.removeClass(rmbdClassess).addClass('ui-body-' + newTheme + ' ui-page-theme-'+ newTheme).attr('data-theme', newTheme); 

    // target the list divider elements, then iterate through them and 
    // change its theme (this is the jQuery Mobile default for 
    // list-dividers) 
    $.mobile.activePage.find('.ui-li-divider').each(function(index, obj) { 
     $(this).removeClass(rmhfClasses).addClass('ui-bar-' + newTheme).attr('data-theme', newTheme); 
    }); 
}; 
+0

I temi di A-S sono qui: [collegamento] (http://www.cnblogs.com/SkyD/p/3999418.html) –

Problemi correlati