Phi*_*enn 9 jquery-mobile data-theme
我想在他们按下按钮后给我的用户提供一些持久的反馈(比如它的缩进或其他东西).我试过了:
$(this).data('theme','b');
Run Code Online (Sandbox Code Playgroud)
但这不起作用.
问:有没有办法显示缩进按钮,或者动态更改它data-theme?
Fra*_*z0r 17
我知道这是一个老问题,但我最近才遇到这个障碍.
这样做的正确方法如下:
$(this).buttonMarkup({theme: 'b'});
Run Code Online (Sandbox Code Playgroud)
我一直在寻找一种在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);
};
Run Code Online (Sandbox Code Playgroud)
还有一些额外的复杂性,因为我还想更新JQM尚未显示的页面主题.以成对的选择器和主题类来构造代码有助于使我更清楚.
您可以调用此函数来动态更新所有UI元素的主题,例如:
$.mobile.changeGlobalTheme("b");
Run Code Online (Sandbox Code Playgroud)
我也喜欢我见过的使用正则表达式的解决方案,但在这种情况下,我更喜欢明确说明选择器和主题类的方法,因为添加新项目的清晰度和易用性.我通过检查DOM树找出了选择器/类对.
我必须说我很欣赏现代JavaScript代码的Lisp风格.