如何在jQuery Mobile中动态更改页面元素的数据主题

时间:2014-03-08 17:34:11

标签: javascript jquery jquery-mobile cordova data-theme

使用jQuery mobile 1.3.2,我有一个PhoneGap应用程序,我想根据应用程序的状态更新初始登录屏幕以反映数据主题。登录页面html是:

登录html

<div id="login" data-role="page">

    <div data-role="header">
        <h1>Survey login</h1>
    </div>



    <div data-role="content">
        <!--div id="logincontent"></div-->

        <form id="form-login">
            <div data-role="fieldcontain" class="ui-hide-label">
                <label for="login-password">Password:</label>
                <input type="password" name="login-password" id="login-password" value="" placeholder="Password" />
            </div>
            <a href="#" id="login-button" data-role="button" onclick="checkLogin()">Login</a>
        </form>
    </div>



    <div data-role="footer" id="login-footer" data-theme="a">
        <h4 id="login-footer-header">UIC &amp; EVL</h4>
    </div>

</div>

JS功能改变登录页面的主题:

    function displayAppStatus(type){
        if(type == 'suspend'){
            $("#login").page({theme:'g'});
            $("#login").trigger('create');
            $("#login-footer-header").text("Log in to break suspension");

        }
        else if(type == 'bedtime'){
            $("#login").page({theme:'f'});
            $("#login").trigger('create');
            $("#login-footer-header").text("Log in to break bedtime");
        }
        else if(type == 'delay'){
            $("#login").page({theme:'h'});
            $("#login").trigger('create');
            $("#login-footer-header").text("Log in to break delayed notification");
        }
        //Cancel appStatus display
        else if(type == 'cancel'){
            $("#login").page({theme:'a'});
            $("#login").trigger('create');
            $("#login-footer-header").text("UIC & EVL");
        }
    }

关联的样式表&amp;脚本(以防万一):

    <link rel="stylesheet" href="css/main.css" />
    <link rel="stylesheet" href="css/themes/ecig/ecig_themes.css" />
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile.structure-1.3.2.css" />

    <script src="cordova.js"></script>

    <script src="js/jquery-1.10.2.js"></script>
    <script src="js/jquery.mobile-1.3.2.js"></script>

如果用户延迟通知,暂停任何通知或让应用程序进入休眠状态,我会在整个代码中调用displayAppStatus。

我将看到登录页面闪烁正确数据主题的颜色,但页面的主题将快速切换回默认值。

我来过这里: Changing JQuery Mobile data-theme dynamicallyjQuery mobile dynamically added theme to page

但这些线程都没有解决我的问题。

1 个答案:

答案 0 :(得分:0)

这非常棘手,特别是因为jquery mobile在代码中创建了很多不可见的CSS来应用它的样式。

我正在粘贴正在运行的代码,允许我根据产品类别更改页面的代码。在这里,我主要更改导航,页眉和页脚样式,但只是将要更改的类传递给函数。

希望它有所帮助!

以下是我如何调用我的函数“Element_theme_refresh”,newTheme基本上是jquery移动主题名称。

function UpdateNavBarStyles ()
{
    var newTheme = AssignMeTheme ( sessionStorage.categoriaID );
    Element_theme_refresh ( '#pag_fichaProducto', newTheme );
    Element_theme_refresh ( '.ui-header', newTheme );
    Element_theme_refresh ( '.ui-footer', newTheme );
    Element_theme_refresh ( '.menuPanelButton', newTheme ); // Once the collapsible exists, update its theme
    Element_theme_refresh ( '.backButton', newTheme );          // Back button, apply new theme
    Element_theme_refresh ( '.quoteBtn', newTheme );            // Quote Cart widget button, update its theme
    Element_theme_refresh ( '.syncroBtn', newTheme );           // Data Syncronization button, update its theme
}

这里是实际的功能。

/* This function query the element for the current applied jquery mobile theme and change
it to the input data-theme or swatch */
function Element_theme_refresh( element, newTheme )
{
    var curTheme = $(element).attr('data-theme') || 'a';

    $(element).attr('data-theme', newTheme);

    if( $(element).attr('class') ) {
        // Theme classes end in "-[a-z]$", so match that
        var classPattern = new RegExp('-' + curTheme + '$');
        newTheme = '-' + newTheme;

        var classes =  $(element).attr('class').split(' ');

        for( var key in classes ) {
            if( classPattern.test( classes[key] ) ) {
                classes[key] = classes[key].replace( classPattern, newTheme );
            }
        }

        $(element).attr('class', classes.join(' '));
    }
}