Jquery mobile 在jQuery mobile中更改数据主题

Jquery mobile 在jQuery mobile中更改数据主题,jquery-mobile,data-theme,Jquery Mobile,Data Theme,我想在我的用户按下按钮(比如缩进或其他)后,给他们一些持续的反馈。我试过: $(this).data('theme','b'); 但这不起作用 问:有没有一种方法可以显示缩进按钮,或者动态更改它的数据主题?也许这对您有用: 我想用按钮也可以用同样的方法完成。我知道这是一个老问题,但我自己最近遇到了这个障碍 正确的方法如下: $(this).buttonMarkup({theme: 'b'}); 对于表单中的submit按钮,我使用jQuery Mobile 1.2.0时可以做到这一点: $

我想在我的用户按下按钮(比如缩进或其他)后,给他们一些持续的反馈。我试过:

$(this).data('theme','b');
但这不起作用


问:有没有一种方法可以显示缩进按钮,或者动态更改它的
数据主题?

也许这对您有用:


我想用按钮也可以用同样的方法完成。

我知道这是一个老问题,但我自己最近遇到了这个障碍

正确的方法如下:

$(this).buttonMarkup({theme: 'b'});

对于表单中的submit按钮,我使用jQuery Mobile 1.2.0时可以做到这一点:

$(this).buttonMarkup({theme: 'b'});
$(this).parent().buttonMarkup({ theme: "b" });

我一直在寻找一种在jQuery Mobile中全局动态更改主题的方法(例如,单击按钮)。结果比我想象的要复杂得多。无论如何,以下是我对这一点的看法,灵感来自SO和其他网站的各种解决方案:

// Dynamically changes the theme of all UI elements on all pages,
// also pages not yet rendered (enhanced) by 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-body", 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);
};
还有一些额外的复杂性,因为我还想更新JQM尚未显示的页面的主题。以选择器和主题类对代码进行结构化有助于我更清楚地理解这一点

您可以调用此函数动态更新所有UI元素的主题,例如:

$.mobile.changeGlobalTheme("b");
我也喜欢我所看到的使用正则表达式的解决方案,但在本例中,我更喜欢显式地使用状态选择器和主题类的方法,因为添加新项既清晰又容易。我通过检查DOM树找出了选择器/类对


我必须说我很欣赏现代JavaScript代码的Lisp风格。

哦,我可以添加和删除class=“ui-btn-up-a,ui-btn-up-b,ui-btn-up-c,ui-btn-up-d,ui-btn-up-e”,这几乎可以正常工作,但jQuery会返回并在之后重新分配相应的ui-btn。您不需要调用。按钮('refresh')感谢Fraz0r!我再次讨论这个问题,因为现在我正在尝试更改列表项的主题,而不是按钮。谢谢Mikael!我不知道我是否和你一样欣赏JavaScript,但我现在对它越来越熟悉了。