當我創建一個jQuery UI元素(按鈕,例如),我不喜歡這樣寫道:在元素的屬性中設置選項?
<button class="Button" data-options='{ "text": false, "icons": { "primary": "ui-icon-arrowthick-1-n" } }'>Move Up</Button>
我有些JS,看起來這通常就是.Button
並提取data-options
屬性爲JSON,然後創建按鈕傳遞選項。
我在想,如果jQuery UI有任何內置函數來完成這個呢?舉例來說,我希望能夠通過把選項的屬性在每個子按鈕設置在Buttonset內的按鈕選項:
<div class="Buttonset">
<input type="radio" id="SortDirection_Ascending" name="SortDirection" value="Ascending" />
<label for="SortDirection_Ascending" title="Ascending" data-options='{ "text": false, "icons": { "primary": "ui-icon-arrowthick-1-n" } }'> </label>
<input type="radio" id="SortDirection_Descending" name="SortDirection" value="Descending" />
<label for="SortDirection_Descending" title="Descending" data-options='{ "text": false, "icons": { "primary": "ui-icon-arrowthick-1-s" } }'> </label>
</div>
但jQuery的不知道要應用的選項。我可以在JS中做這個,就像我使用按鈕(和其他小部件)一樣。但我很好奇,如果這已經支持一個不同的屬性。
澄清:
當我創建窗口小部件,我想知道是否jQuery的可以自動從元件的一個屬性提取窗口小部件的選項,並將其應用到小部件。
<button class="Button" data-options='{ "text": false, "icons": { "primary": "ui-icon-arrowthick-1-n" } }'>Move Up</Button>
<script type="text/javascript">
$(".Button").button();
/* jQuery saw that this element has a "data-options" attribute and automatically
* extracted the options from this attribute and applied them to the widget. */
</script>
還是你的問題還不清楚。您將能夠以JSON的身份訪問它們,是的。 :) –
您的筆記「我有一些JS查找任何.Button」和「我無法爲Buttonset做這件事」似乎是矛盾的。你能解釋爲什麼你不能爲div做這件事嗎? –
我想知道是否有一個特定的屬性名稱,我可以使用哪個jQuery檢查並應用其中包含的選項。 –