动态更改 jquery 移动色板
[英] change jquery mobile color swatch dynamically
本文介绍了动态更改 jquery 移动色板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用 javascript 动态更改 jquery-mobile 按钮颜色样本,但我找不到方法(除了删除和添加所有类和事件处理程序,但这太麻烦了).
最好的方法是什么?
解决方案
好吧,我做了这样的事情:
JS(可能需要稍作调整才能得到您想要的)
$('input[name=theme-options]').change(function() {var currentTheme = $('#home').attr('data-theme');var selectedTheme = $(this).val();alert('CT'+currentTheme+'ST'+selectedTheme);$('.ui-body-' + currentTheme).each(function(){$(this).removeClass('ui-body-' + currentTheme).addClass('ui-body-' + selectedTheme);});$('.ui-btn-up-' + currentTheme).each(function(){$(this).removeClass('ui-btn-up-' + currentTheme).addClass('ui-btn-up-' + selectedTheme);});$('.ui-btn-down-' + currentTheme).each(function(){$(this).removeClass('ui-btn-down-' + currentTheme).addClass('ui-btn-down-' + selectedTheme);});$('#home').find('*[data-theme]').each(function(index){$(this).attr('data-theme',selectedTheme);});$('#home').attr('data-theme', selectedTheme).removeClass('ui-body-' + currentTheme).addClass('ui-body-' + selectedTheme).trigger('create');});
HTML(可能需要稍作调整才能得到您想要的)
<div data-role="内容"><div class="content-primary"><p><div data-role="fieldcontain"><fieldset data-role="控制组"><legend>选择一个主题:</legend><input type="radio" name="theme-options" id="theme-option-a" value="a" checked="checked" data-theme="a"/><label for="theme-option-a" data-theme="a">主题A</label><input type="radio" name="theme-options" id="theme-option-b" value="b" data-theme="b"/><label for="theme-option-b" data-theme="b">主题B</label><input type="radio" name="theme-options" id="theme-option-c" value="c" data-theme="c"/><label for="theme-option-c" data-theme="c">主题C</label><input type="radio" name="theme-options" id="theme-option-d" value="d" data-theme="d"/><label for="theme-option-d" data-theme="d">主题 D</label><input type="radio" name="theme-options" id="theme-option-e" value="e" data-theme="e"/><label for="theme-option-e" data-theme="e">主题 E</label></fieldset>
</p><form action="#" method="get"><h2>表单元素</h2><div data-role="fieldcontain"><label for="name">文本输入:</label><input type="text" name="name" id="name" value=""/>
<div data-role="fieldcontain"><label for="textarea">Textarea:</label><textarea cols="40" rows="8" name="textarea" id="textarea"></textarea>
<div data-role="fieldcontain"><label for="search">搜索输入:</label><input type="search" name="password" id="search" value=""/>
<div data-role="fieldcontain"><label for="slider2">翻转开关:</label><select name="slider2" id="slider2" data-role="slider"><option value="off">Off</option><option value="on">On</option></选择>
<div data-role="fieldcontain"><label for="slider">滑块:</label><input type="range" name="slider" id="slider" value="0" min="0" max="100"/>
<div data-role="fieldcontain"><fieldset data-role="控制组"><legend>选择你喜欢的零食:</legend><input type="checkbox" name="checkbox-1a" id="checkbox-1a" class="custom"/><label for="checkbox-1a">Cheetos</label><input type="checkbox" name="checkbox-2a" id="checkbox-2a" class="custom"/><label for="checkbox-2a">Doritos</label><input type="checkbox" name="checkbox-3a" id="checkbox-3a" class="custom"/><label for="checkbox-3a">Fritos</label><input type="checkbox" name="checkbox-4a" id="checkbox-4a" class="custom"/><label for="checkbox-4a">Sun Chips</label></fieldset>
<div data-role="fieldcontain"><fieldset data-role="controlgroup" data-type="horizontal"><legend>字体样式:</legend><input type="checkbox" name="checkbox-6" id="checkbox-6" class="custom"/><label for="checkbox-6">b</label><input type="checkbox" name="checkbox-7" id="checkbox-7" class="custom"/><label for="checkbox-7"><em>i</em></label><input type="checkbox" name="checkbox-8" id="checkbox-8" class="custom"/><label for="checkbox-8">u</label></fieldset>
<div data-role="fieldcontain"><fieldset data-role="控制组"><legend>选择宠物:</legend><input type="radio" name="radio-choice-1" id="radio-choice-1" value="choice-1" checked="checked"/><label for="radio-choice-1">猫</label><input type="radio" name="radio-choice-1" id="radio-choice-2" value="choice-2"/><label for="radio-choice-2">狗</label><input type="radio" name="radio-choice-1" id="radio-choice-3" value="choice-3"/><label for="radio-choice-3">Hamster</label><input type="radio" name="radio-choice-1" id="radio-choice-4" value="choice-4"/><label for="radio-choice-4">蜥蜴</label></fieldset>
<div data-role="fieldcontain"><fieldset data-role="controlgroup" data-type="horizontal"><legend>布局视图:</legend><input type="radio" name="radio-choice-b" id="radio-choice-c" value="on" checked="checked"/><label for="radio-choice-c">列表</label><input type="radio" name="radio-choice-b" id="radio-choice-d" value="off"/><label for="radio-choice-d">网格</label><input type="radio" name="radio-choice-b" id="radio-choice-e" value="other"/><label for="radio-choice-e">图库</label></fieldset>
<div data-role="fieldcontain"><label for="select-choice-1" class="select">选择配送方式:</label><select name="select-choice-1" id="select-choice-1"><option value="standard">标准:7天</option><option value="rush">Rush: 3 天</option><option value="express">快递:第二天</option><option value="overnight">过夜</option></选择>
<div data-role="fieldcontain"><label for="select-choice-3" class="select">您的状态:</label><select name="select-choice-3" id="select-choice-3"><option value="AL">阿拉巴马州</option><option value="AK">阿拉斯加</option><option value="AZ">Arizona</option><option value="AR">阿肯色州</option><option value="CA">加利福尼亚</option><option value="CO">科罗拉多</option><option value="CT">康涅狄格</option><option value="DE">特拉华州</option><option value="FL">佛罗里达</option><option value="GA">格鲁吉亚</option><option value="HI">夏威夷</option><option value="ID">爱达荷州</option><option value="IL">伊利诺伊州</option><option value="IN">印第安纳州</option><option value="IA">爱荷华</option><option value="KS">堪萨斯</option><option value="KY">肯塔基</option><option value="LA">路易斯安那</option><option value="ME">缅因州</option><option value="MD">马里兰</option><option value="MA">马萨诸塞州</option><option value="MI">密歇根</option><option value="MN">明尼苏达</option><option value="MS">密西西比</option><option value="MO">密苏里</option><option value="MT">蒙大拿</option><option value="NE">内布拉斯加州</option><option value="NV">内华达</option><option value="NH">新罕布什尔州</option><option value="NJ">新泽西</option><option value="NM">新墨西哥</option><option value="NY">纽约</option><option value="NC">北卡罗来纳</option><option value="ND">北达科他州</option><option value="OH">Ohio</option><option value="OK">俄克拉荷马</option><option value="OR">俄勒冈</option><option value="PA">宾夕法尼亚</option><option value="RI">罗德岛</option><option value="SC">南卡罗来纳州</option><option value="SD">南达科他州</option><option value="TN">田纳西</option><option value="TX">德克萨斯</option><option value="UT">犹他州</option><option value="VT">佛蒙特</option><option value="VA">弗吉尼亚</option><option value="WA">华盛顿</option><option value="WV">西弗吉尼亚</option><option value="WI">威斯康星州</option><option value="WY">怀俄明</option></选择>
<div data-role="fieldcontain"><label for="select-choice-a" class="select">选择配送方式:</label><select name="select-choice-a" id="select-choice-a" data-native-menu="false"><option>自定义菜单示例</option><option value="standard">标准:7天</option><option value="rush">Rush: 3 天</option><option value="express">快递:第二天</option><option value="overnight">过夜</option></选择>
<div class="ui-body ui-body-b"><fieldset class="ui-grid-a"><div class="ui-block-a"><button type="submit" data-theme="d">取消</button></div><div class="ui-block-b"><button type="submit" data-theme="a">Submit</button></div></fieldset>
</表单></div><!--/content-primary --><div class="content-secondary"><div data-role="collapsible" data-collapsed="true" data-theme="b"><h3>更多在本节</h3><ul data-role="listview" data-theme="c" data-dividertheme="d"><li data-role="list-divider">表单元素</li><li><a href="docs-forms.html">表单基础</a></li><li data-theme="a"><a href="forms-all.html">表单元素库</a></li><li><a href="texts/index.html">文本输入</a></li><li><a href="forms-search.html">搜索输入</a></li><li><a href="forms-slider.html">滑块</a></li><li><a href="forms-switch.html">翻转拨动开关</a></li><li><a href="radiobuttons/index.html">单选按钮</a></li><li><a href="checkboxes/index.html">复选框</a></li><li><a href="selects/index.html">选择菜单</a></li><li><a href="forms-themes.html">主题表单</a></li><li><a href="forms-all-native.html">原生表单元素</a></li><li><a href="forms-sample.html">提交表单</a></li><li><a href="plugin-eventsmethods.html">插件方法</a></li>
I want to change a jquery-mobile buttons color swatch dynamically with javascript, but I can't' find a way (except removing and adding all classes and event handlers, but that's to messy).
Whats the best way to do that?
解决方案
Well I did do something like this:
JS (might need minor tweaks to get what you want)
$('input[name=theme-options]').change(function() {
var currentTheme = $('#home').attr('data-theme');
var selectedTheme = $(this).val();
alert('CT '+currentTheme+' ST '+selectedTheme);
$('.ui-body-' + currentTheme).each(function(){
$(this).removeClass('ui-body-' + currentTheme).addClass('ui-body-' + selectedTheme);
});
$('.ui-btn-up-' + currentTheme).each(function(){
$(this).removeClass('ui-btn-up-' + currentTheme).addClass('ui-btn-up-' + selectedTheme);
});
$('.ui-btn-down-' + currentTheme).each(function(){
$(this).removeClass('ui-btn-down-' + currentTheme).addClass('ui-btn-down-' + selectedTheme);
});
$('#home').find('*[data-theme]').each(function(index){
$(this).attr('data-theme',selectedTheme);
});
$('#home').attr('data-theme', selectedTheme).removeClass('ui-body-' + currentTheme).addClass('ui-body-' + selectedTheme).trigger('create');
});
HTML (might need minor tweaks to get what you want)
<div data-role="page" id="home" data-theme="a">
<div data-role="content">
<div class="content-primary">
<p>
<div data-role="fieldcontain">
<fieldset data-role="controlgroup">
<legend>Choose a Theme:</legend>
<input type="radio" name="theme-options" id="theme-option-a" value="a" checked="checked" data-theme="a" />
<label for="theme-option-a" data-theme="a">Theme A</label>
<input type="radio" name="theme-options" id="theme-option-b" value="b" data-theme="b" />
<label for="theme-option-b" data-theme="b">Theme B</label>
<input type="radio" name="theme-options" id="theme-option-c" value="c" data-theme="c" />
<label for="theme-option-c" data-theme="c">Theme C</label>
<input type="radio" name="theme-options" id="theme-option-d" value="d" data-theme="d" />
<label for="theme-option-d" data-theme="d">Theme D</label>
<input type="radio" name="theme-options" id="theme-option-e" value="e" data-theme="e" />
<label for="theme-option-e" data-theme="e">Theme E</label>
</fieldset>
</div>
</p>
<form action="#" method="get">
<h2>Form elements</h2>
<div data-role="fieldcontain">
<label for="name">Text Input:</label>
<input type="text" name="name" id="name" value="" />
</div>
<div data-role="fieldcontain">
<label for="textarea">Textarea:</label>
<textarea cols="40" rows="8" name="textarea" id="textarea"></textarea>
</div>
<div data-role="fieldcontain">
<label for="search">Search Input:</label>
<input type="search" name="password" id="search" value="" />
</div>
<div data-role="fieldcontain">
<label for="slider2">Flip switch:</label>
<select name="slider2" id="slider2" data-role="slider">
<option value="off">Off</option>
<option value="on">On</option>
</select>
</div>
<div data-role="fieldcontain">
<label for="slider">Slider:</label>
<input type="range" name="slider" id="slider" value="0" min="0" max="100" />
</div>
<div data-role="fieldcontain">
<fieldset data-role="controlgroup">
<legend>Choose as many snacks as you'd like:</legend>
<input type="checkbox" name="checkbox-1a" id="checkbox-1a" class="custom" />
<label for="checkbox-1a">Cheetos</label>
<input type="checkbox" name="checkbox-2a" id="checkbox-2a" class="custom" />
<label for="checkbox-2a">Doritos</label>
<input type="checkbox" name="checkbox-3a" id="checkbox-3a" class="custom" />
<label for="checkbox-3a">Fritos</label>
<input type="checkbox" name="checkbox-4a" id="checkbox-4a" class="custom" />
<label for="checkbox-4a">Sun Chips</label>
</fieldset>
</div>
<div data-role="fieldcontain">
<fieldset data-role="controlgroup" data-type="horizontal">
<legend>Font styling:</legend>
<input type="checkbox" name="checkbox-6" id="checkbox-6" class="custom" />
<label for="checkbox-6">b</label>
<input type="checkbox" name="checkbox-7" id="checkbox-7" class="custom" />
<label for="checkbox-7"><em>i</em></label>
<input type="checkbox" name="checkbox-8" id="checkbox-8" class="custom" />
<label for="checkbox-8">u</label>
</fieldset>
</div>
<div data-role="fieldcontain">
<fieldset data-role="controlgroup">
<legend>Choose a pet:</legend>
<input type="radio" name="radio-choice-1" id="radio-choice-1" value="choice-1" checked="checked" />
<label for="radio-choice-1">Cat</label>
<input type="radio" name="radio-choice-1" id="radio-choice-2" value="choice-2" />
<label for="radio-choice-2">Dog</label>
<input type="radio" name="radio-choice-1" id="radio-choice-3" value="choice-3" />
<label for="radio-choice-3">Hamster</label>
<input type="radio" name="radio-choice-1" id="radio-choice-4" value="choice-4" />
<label for="radio-choice-4">Lizard</label>
</fieldset>
</div>
<div data-role="fieldcontain">
<fieldset data-role="controlgroup" data-type="horizontal">
<legend>Layout view:</legend>
<input type="radio" name="radio-choice-b" id="radio-choice-c" value="on" checked="checked" />
<label for="radio-choice-c">List</label>
<input type="radio" name="radio-choice-b" id="radio-choice-d" value="off" />
<label for="radio-choice-d">Grid</label>
<input type="radio" name="radio-choice-b" id="radio-choice-e" value="other" />
<label for="radio-choice-e">Gallery</label>
</fieldset>
</div>
<div data-role="fieldcontain">
<label for="select-choice-1" class="select">Choose shipping method:</label>
<select name="select-choice-1" id="select-choice-1">
<option value="standard">Standard: 7 day</option>
<option value="rush">Rush: 3 days</option>
<option value="express">Express: next day</option>
<option value="overnight">Overnight</option>
</select>
</div>
<div data-role="fieldcontain">
<label for="select-choice-3" class="select">Your state:</label>
<select name="select-choice-3" id="select-choice-3">
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
<option value="FL">Florida</option>
<option value="GA">Georgia</option>
<option value="HI">Hawaii</option>
<option value="ID">Idaho</option>
<option value="IL">Illinois</option>
<option value="IN">Indiana</option>
<option value="IA">Iowa</option>
<option value="KS">Kansas</option>
<option value="KY">Kentucky</option>
<option value="LA">Louisiana</option>
<option value="ME">Maine</option>
<option value="MD">Maryland</option>
<option value="MA">Massachusetts</option>
<option value="MI">Michigan</option>
<option value="MN">Minnesota</option>
<option value="MS">Mississippi</option>
<option value="MO">Missouri</option>
<option value="MT">Montana</option>
<option value="NE">Nebraska</option>
<option value="NV">Nevada</option>
<option value="NH">New Hampshire</option>
<option value="NJ">New Jersey</option>
<option value="NM">New Mexico</option>
<option value="NY">New York</option>
<option value="NC">North Carolina</option>
<option value="ND">North Dakota</option>
<option value="OH">Ohio</option>
<option value="OK">Oklahoma</option>
<option value="OR">Oregon</option>
<option value="PA">Pennsylvania</option>
<option value="RI">Rhode Island</option>
<option value="SC">South Carolina</option>
<option value="SD">South Dakota</option>
<option value="TN">Tennessee</option>
<option value="TX">Texas</option>
<option value="UT">Utah</option>
<option value="VT">Vermont</option>
<option value="VA">Virginia</option>
<option value="WA">Washington</option>
<option value="WV">West Virginia</option>
<option value="WI">Wisconsin</option>
<option value="WY">Wyoming</option>
</select>
</div>
<div data-role="fieldcontain">
<label for="select-choice-a" class="select">Choose shipping method:</label>
<select name="select-choice-a" id="select-choice-a" data-native-menu="false">
<option>Custom menu example</option>
<option value="standard">Standard: 7 day</option>
<option value="rush">Rush: 3 days</option>
<option value="express">Express: next day</option>
<option value="overnight">Overnight</option>
</select>
</div>
<div class="ui-body ui-body-b">
<fieldset class="ui-grid-a">
<div class="ui-block-a"><button type="submit" data-theme="d">Cancel</button></div>
<div class="ui-block-b"><button type="submit" data-theme="a">Submit</button></div>
</fieldset>
</div>
</form>
</div><!--/content-primary -->
<div class="content-secondary">
<div data-role="collapsible" data-collapsed="true" data-theme="b">
<h3>More in this section</h3>
<ul data-role="listview" data-theme="c" data-dividertheme="d">
<li data-role="list-divider">Form elements</li>
<li><a href="docs-forms.html">Form basics</a></li>
<li data-theme="a"><a href="forms-all.html">Form element gallery</a></li>
<li><a href="texts/index.html">Text inputs</a></li>
<li><a href="forms-search.html">Search inputs</a></li>
<li><a href="forms-slider.html">Slider</a></li>
<li><a href="forms-switch.html">Flip toggle switch</a></li>
<li><a href="radiobuttons/index.html">Radio buttons</a></li>
<li><a href="checkboxes/index.html">Checkboxes</a></li>
<li><a href="selects/index.html">Select menus</a></li>
<li><a href="forms-themes.html">Theming forms</a></li>
<li><a href="forms-all-native.html">Native form elements</a></li>
<li><a href="forms-sample.html">Submitting forms</a></li>
<li><a href="plugin-eventsmethods.html">Plugin methods</a></li>
</ul>
</div>
</div>
</div>
</div>
这篇关于动态更改 jquery 移动色板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文