在 jQuery Mobile 中建立水平選擇控制元件組


水平選擇控制元件組是一個介面元件,允許使用者從水平排列的一系列選項中進行選擇。

步驟 1:在專案中包含 jQuery Mobile

建立水平選擇控制元件組的第一步是在專案中包含 jQuery Mobile。為此,您可以直接將 jQuery Mobile 的最新版本新增到您的 HTML 頁面,或者透過內容分發網路 (CDN) 來實現。

演算法

  • 從給定的 CDN URL 載入 jQuery Mobile CSS 檔案

  • 從給定的 CDN URL 載入 jQuery 庫

  • 從給定的 CDN URL 載入 jQuery Mobile JS 檔案

示例

<!-- Include jQuery Mobile CSS -->
<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">

<!-- Include jQuery Mobile JS -->
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>

步驟 2:建立水平選擇控制元件組

將 jQuery Mobile 新增到您的專案後,使用 HTML 標記新增水平選擇控制元件組。

演算法

  • 使用 `data-role` 屬性設定為 "controlgroup" 和 `data-type` 屬性設定為 "horizontal" 來標識容器元素。

  • 在容器元素內建立子元素來表示各個選項。

  • 對於每個選項,建立一個具有以下屬性的元素:

    • `href` 屬性設定為連結 URL(在本例中為 "#")。

    • `class` 屬性設定為 "ui-btn ui-corner-all ui-shadow"。

    • 表示選項標籤的文字內容。

    • 對每個選項重複步驟 3。

    • 將元素作為子元素放置在容器元素內。

    • 使用 CSS 隨意設定選項和容器元素的樣式。

示例

<div data-role="controlgroup" data-type="horizontal">
  <a href="#" class="ui-btn ui-corner-all ui-shadow" data-icon="star">Option 1</a>
  <a href="#" class="ui-btn ui-corner-all ui-shadow" data-icon="check">Option 2</a>
  <a href="#" class="ui-btn ui-corner-all ui-shadow">Option 3</a>
  <a href="#" class="ui-btn ui-corner-all ui-shadow">Option 4</a>
  <a href="#" class="ui-btn ui-corner-all ui-shadow">Option 5</a>
</div>

步驟 3:初始化水平選擇控制元件組

為了使水平選擇控制元件組正常工作,我們必須使用 JavaScript 初始化它。

演算法

  • 文件建立或載入時呼叫該函式。

  • 該函式選擇所有 `data-role` 屬性等於 "controlgroup" 的 div 元素。

  • 在選定的元素上呼叫 "controlgroup()" 函式以初始化控制元件組。

  • 呼叫控制元件組上的 "container()" 方法以檢索其容器元素。

  • 在容器元素上呼叫 "enhanceWithin()" 方法以增強其子元素。

  • 在增強的容器元素上呼叫 "css()" 方法以將其寬度屬性設定為 100%。

示例

$(document).on("pagecreate", function() {
  $("div[data-role='controlgroup']").controlgroup().controlgroup("container").enhanceWithin().css("width", "100%");
});

結論

為了獲得最佳使用者體驗,必須考慮某些約束條件。

  • 每個選項的寬度必須相等,以保持一致的顯示效果。

  • 選項的數量應限制在可用的螢幕空間內,以避免重疊和滾動。

  • 建議在大螢幕上而不是小螢幕上使用水平選擇控制元件組。

  • 在各種裝置和螢幕尺寸上進行徹底測試,以確保控制元件組具有響應能力並能正常執行至關重要。

記住這些約束條件,開發人員可以建立水平選擇控制元件組,從而增強移動 Web 應用程式的可用性和功能。

更新於:2023年8月21日

瀏覽量:90

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.