jQuery mobile dynamically added theme to page

前端 未结 2 1006
走了就别回头了
走了就别回头了 2020-12-20 09:30

I\'m building a new page dynamically using jQuery mobile. I would like to now add the theme i.e. data-theme=\"a\" . Is there an easier way to do this? At the m

相关标签:
2条回答
  • 2020-12-20 10:11

    I use this and it works really great :D

    Mikael Kindborg answer from Change data-theme in jQuery mobile

    $.mobile.changeGlobalTheme = function(theme)
        {
            // These themes will be cleared, add more
            // swatch letters as needed.
            var themes = " a b c d e";
    
            // Updates the theme for all elements that match the
            // CSS selector with the specified theme class.
            function setTheme(cssSelector, themeClass, theme)
            {
                $(cssSelector)
                        .removeClass(themes.split(" ").join(" " + themeClass + "-"))
                        .addClass(themeClass + "-" + theme)
                        .attr("data-theme", theme);
            }
    
            // Add more selectors/theme classes as needed.
            setTheme(".ui-mobile-viewport", "ui-overlay", theme);
            setTheme("[data-role='page']", "ui-page-theme", theme);
            setTheme("[data-role='header']", "ui-bar", theme);
            setTheme("[data-role='listview'] > li", "ui-bar", theme);
            setTheme(".ui-btn", "ui-btn-up", theme);
            setTheme(".ui-btn", "ui-btn-hover", theme);
        };
    
    0 讨论(0)
  • 2020-12-20 10:23

    Update - jQuery Mobile 1.4

    There is a bug in .page() widget when using .page({"theme"}) or .page("option", "theme"). It doesn't remove current theme class, however, it adds new theme class. Yet, new class doesn't override old class. So here is the fix.

    (function($, undefined) {
      $.widget("mobile.page", $.mobile.page, {
        _setOptions: function(o) {
          if (o.theme !== undefined) {
            this.element
              .removeClass(function(i, c) {
                return (c.match(/\bui-page-theme-\w/g) || []).join(' ');
              })
              .addClass("ui-page-theme-" + o.theme);
          }
        }
      });
    })(jQuery);
    

    Note: Default theme in jQuery Mobile is "a".


    To set theme for dynamically created pages, use $('.selector').page({theme:'e'}); after you append pages and before you navigate to them using $.mobile.changePage().

    For a specific page:

    $('.selector').page({theme:'e'});
    

    For all pages:

    $('[data-role=page]').page({theme:'e'});
    

    Demo - Updated with .page() fix

    0 讨论(0)
提交回复
热议问题