2011-12-28 8 views
16

jQuery Mobileを使用してモバイルWebアプリケーションを作成しています。jquery mobileでテーマを動的に変更する方法は?

私はすべてのページにtheme-bを使用しています。私はすべてのページに対して別のテーマに動的に変更したいと思います。テーマを動的に変更するにはどうすればよいですか?

答えて

20

あなたは、特定のウィジェットに関連する特定のクラスをターゲット授業をリセットした後、あなたが選んだのテーマにクラスを追加することができます。

//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/

これは決して完全に機能コードですテーマを切り替えて上記のコードに追加するときに更新したいウィジェットを見つける必要があります。 FireBugや他のデベロッパーコンソールを使用して、各ウィジェットがどのクラスを簡単に見つけられるかを見つけることができます。あなたのアカウントにdata-role="list-divider要素を取るとき、これは少しトリッキー取得

UPDATE

:ここ

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*/ 

はデモです:http://jsfiddle.net/VNXb2/7/

+0

(再び)jQueryのモバイル1.4.5に最適です。 –

+0

@kanna私の答えを更新しました。 'data-role =" list-divider "はそれらを正しく得るには少しの論理が必要ですが、私の答えはかなり完成していると思います:http://jsfiddle.net/VNXb2/7/ – Jasper

+0

@kannaウィジェットが初期化される前にウィジェットのテーマを変更すると、それはあなたの選択したテーマで正しく初期化されます。 'pageInit'イベントにバインドすると、ウィジェットは初期化されるべきではなく、ページのテーマやページ内の要素を変更することができます。 – Jasper

9

上記の答えは私をたくさん助けました、私はthemerollerを使用していて、20以上のテーマを持つことを期待しているので、私のニーズに少し修正しました。ここで私は、私はJSONを介してサーバから新しいテーマを得るとき今、私はちょうどのparamなどの新しいテーマに、このメソッドを呼び出す

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); 

}) 

を行っているものです。

よろしく ラジェッシュJ

+0

のテーマを変更するにはどうしたらいいですか?ここにどのクラスを追加する必要がありますか? – Prasoon

0

ラジェッシュの答えは私をたくさん助けた...しかし、ラジェッシュは、あなたは「* UI-ページTHEME-」----重要なクラスを逃したので、私は今、あなたの答えを修正し、それは私が静的にそれが変化しているが、動的に、それはそれが唯一の問題では変更されません私のアプリケーションでは、リスト分圧器を使用してメートル...

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

A-Sのテーマは次のとおりです。[link](http://www.cnblogs.com/SkyD/p/3999418.html) –

関連する問題