當咱們使用Ajax或者javascript動態在頁面上添加元素後,如添加select控件的option元素,添加ul的li元素。添加以後咱們會發現,它們顯示的樣式不是jQuery Mobile的炫酷樣式了,而變成了很醜陋的元素樣式。以下圖所示:javascript
代碼以下:java
1 <script> 2 function myFunction() { 3 var ul = document.getElementById("myul"); 4 var li1 = "<li data-role=\"fieldcontain\">信息2</li>"; 5 var li2 = "<li data-role=\"fieldcontain\">信息3</li>"; 6 ul.innerHTML += li1; 7 ul.innerHTML += li2; 8 } 9 </script> 10 11 <!-- listview測試 --> 12 <ul data-role="listview" data-inset="true" id="myul"> 13 <li data-role="list-divider">信息列表</li> 14 <li data-role="fieldcontain">信息1</li> 15 </ul>
能夠看到,jQuery Mobile並無對於新添加的元素進行渲染。這種狀況下就須要對元素的樣式進行刷新。app
刷新代碼以下:ide
1 <script> 2 function myFunction() { 3 var ul = document.getElementById("myul"); 4 var li1 = "<li data-role=\"fieldcontain\">信息2</li>"; 5 var li2 = "<li data-role=\"fieldcontain\">信息3</li>"; 6 ul.innerHTML += li1; 7 ul.innerHTML += li2; 8 9 //刷新jQuery Mobile樣式 10 $('#myul').listview('refresh'); 11 } 12 </script>
其實最重要的一句刷新代碼是:$('#myul').listview('refresh');測試
刷新後的效果以下圖所示:spa
注意:在使用js或者jQuery獲取控件(例如:button、checkbox、radiobutton等)的值時,也是須要先刷新,不然沒法獲取到最新的值。code
下面列出經常使用的標籤的refresh操做,其餘的能夠觸類旁通。
1. Listview的refresh操做:
$('#mylistid').listview('refresh');blog
2. select menu的refresh操做:
var myselect = $("#myselect");
myselect[0].selectedIndex = 2;
myselect.selectmenu("refresh");ip
3. Checkboxes的refresh操做:
$("#mycheckboxid").attr("checked",true).checkboxradio("refresh");ci
4. Radio buttons的refresh操做:
$("#myradioid").attr("checked",true).checkboxradio("refresh");
新加的:來自:http://hi.baidu.com/life_to_you/item/bf3621365fa5974b033edcbc
各種標籤的刷新
1.Textarea fields
1
2
|
$(
'body'
).prepend(
'<textarea id="myTextArea"></textarea>'
);
$(
'#myTextArea'
).textinput();
|
2.Text input fields
1
2
|
$(
'body'
).prepend(
'<input type="text" id="myTextField" />'
);
$(
'#myTextField'
).textinput();
|
3.Buttons
1
2
|
$(
'body'
).append(
'<a href="" data-theme="e" id="myNewButton">testing</a>'
);
$(
'#myNewButton'
).button();
|
4.Combobox or select dropdowns
1
2
3
4
5
6
7
8
9
10
|
<label
for
=
"sCountry"
>Country:</label>
<select name=
"sCountry"
id=
"sCountry"
>
<option value=
""
>Where You Live:</option>
<option value=
"ad"
>Andorra</option>
<option value=
"ae"
>United Arab Emirates</option>
</select>
var
myselect = $(
"#sCountry"
);
myselect[0].selectedIndex = 3;
myselect.selectmenu(
'refresh'
);
|
5.Listviews
1
2
3
4
5
6
7
|
<ul id=
"myList"
data-role=
"listview"
data-inset=
"true"
>
<li>Acura</li>
<li>Audi</li>
<li>BMW</li>
</ul>
$(
'#mylist'
).listview(
'refresh'
);
|
6.Slider control
1
2
3
4
5
6
|
<div data-role=
"fieldcontain"
>
<label
for
=
"slider-2"
>Input slider:</label>
<input type=
"range"
id=
"slider-2"
value=
"25"
min=
"0"
max=
"100"
/>
</div>
$(
'#slider-2'
).val(80).slider(
'refresh'
);
|
7.Toggle switch
1
2
3
4
5
6
7
8
9
10
11
|
<div data-role=
"fieldcontain"
>
<label
for
=
"toggle"
>Flip
switch
:</label>
<select name=
"toggle"
id=
"toggle"
data-role=
"slider"
>
<option value=
"off"
>Off</option>
<option value=
"on"
>On</option>
</select>
</div>
var
myswitch = $(
"#toggle"
);
myswitch[0].selectedIndex = 1;
myswitch .slider(
"refresh"
);
|
8.Radio buttons
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<div data-role=
"fieldcontain"
>
<fieldset data-role=
"controlgroup"
data-type=
"horizontal"
>
<legend>Layout view:</legend>
<input type=
"radio"
name=
"radio-view"
value=
"list"
/>
<label
for
=
"radio-view-a"
>List</label>
<input type=
"radio"
name=
"radio-view"
value=
"grid"
/>
<label
for
=
"radio-view-b"
>Grid</label>
<input type=
"radio"
name=
"radio-view"
value=
"gallery"
/>
<label
for
=
"radio-view-c"
>Gallery</label>
</fieldset>
</div>
$(
"input[value=grid]"
).attr(
'checked'
,
true
).checkboxradio(
'refresh'
);
|
9.Checkboxes
1
2
3
4
5
6
7
8
9
|
<div data-role=
"fieldcontain"
>
<fieldset data-role=
"controlgroup"
>
<legend>Agree to the terms:</legend>
<input type=
"checkbox"
name=
"checkbox-1"
id=
"checkbox-1"
class=
"custom"
/>
<label
for
=
"checkbox-1"
>I agree</label>
</fieldset>
</div>
$(
'#checkbox-1'
).attr(
'checked'
,
true
).checkboxradio(
'refresh'
);
|