选择(Selectable)


jQuery UI 实例 - 选择(Selectable)

使用鼠标选择单个元素或一组元素。

如需了解更多有关 selectable 交互的细节,请查看 API 文档 可选择小部件(Selectable Widget)。

默认功能

在某个 DOM 元素上或者一组元素上启用 selectable 功能。通过鼠标拖拽选择条目。按住 Ctrl 键,选择多个不相邻的条目。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 选择(Selectable) - 默认功能</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.  
  11.   <style>
  12.   #feedback { font-size: 1.4em; }
  13.   #selectable .ui-selecting { background: #FECA40; }
  14.   #selectable .ui-selected { background: #F39814; color: white; }
  15.   #selectable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
  16.   #selectable li { margin: 3px; padding: 0.4em; font-size: 1.4em; height: 18px; }
  17.   </style>
  18.   <script>
  19.   $(function() {
  20.     $( "#selectable" ).selectable();
  21.   });
  22.   </script>
  23. </head>
  24. <body>
  25.  
  26. <ol id="selectable">
  27.   <li class="ui-widget-content">Item 1</li>
  28.   <li class="ui-widget-content">Item 2</li>
  29.   <li class="ui-widget-content">Item 3</li>
  30.   <li class="ui-widget-content">Item 4</li>
  31.   <li class="ui-widget-content">Item 5</li>
  32.   <li class="ui-widget-content">Item 6</li>
  33.   <li class="ui-widget-content">Item 7</li>
  34. </ol>
  35.  
  36.  
  37. </body>
  38. </html>


显示为网格

让 selectable 条目显示为网格,使用 CSS 使得它们带有相同的尺寸且浮动显示。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 选择(Selectable) - 显示为网格</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.  
  11.   <style>
  12.   #feedback { font-size: 1.4em; }
  13.   #selectable .ui-selecting { background: #FECA40; }
  14.   #selectable .ui-selected { background: #F39814; color: white; }
  15.   #selectable { list-style-type: none; margin: 0; padding: 0; width: 450px; }
  16.   #selectable li { margin: 3px; padding: 1px; float: left; width: 100px; height: 80px; font-size: 4em; text-align: center; }
  17.   </style>
  18.   <script>
  19.   $(function() {
  20.     $( "#selectable" ).selectable();
  21.   });
  22.   </script>
  23. </head>
  24. <body>
  25.  
  26. <ol id="selectable">
  27.   <li class="ui-state-default">1</li>
  28.   <li class="ui-state-default">2</li>
  29.   <li class="ui-state-default">3</li>
  30.   <li class="ui-state-default">4</li>
  31.   <li class="ui-state-default">5</li>
  32.   <li class="ui-state-default">6</li>
  33.   <li class="ui-state-default">7</li>
  34.   <li class="ui-state-default">8</li>
  35.   <li class="ui-state-default">9</li>
  36.   <li class="ui-state-default">10</li>
  37.   <li class="ui-state-default">11</li>
  38.   <li class="ui-state-default">12</li>
  39. </ol>
  40.  
  41.  
  42. </body>
  43. </html>


序列化

写一个函数,在 stop 事件发生时触发,来收集被选中条目的索引值。呈现这些值作为反馈,或者以数据字符串形式进行传递。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 选择(Selectable) - 序列化</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.  
  11.   <style>
  12.   #feedback { font-size: 1.4em; }
  13.   #selectable .ui-selecting { background: #FECA40; }
  14.   #selectable .ui-selected { background: #F39814; color: white; }
  15.   #selectable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
  16.   #selectable li { margin: 3px; padding: 0.4em; font-size: 1.4em; height: 18px; }
  17.   </style>
  18.   <script>
  19.   $(function() {
  20.     $( "#selectable" ).selectable({
  21.       stop: function() {
  22.         var result = $( "#select-result" ).empty();
  23.         $( ".ui-selected", this ).each(function() {
  24.           var index = $( "#selectable li" ).index( this );
  25.           result.append( " #" + ( index + 1 ) );
  26.         });
  27.       }
  28.     });
  29.   });
  30.   </script>
  31. </head>
  32. <body>
  33.  
  34. <p id="feedback">
  35. <span>您已经选择了:</span> <span id="select-result"></span>
  36. </p>
  37.  
  38. <ol id="selectable">
  39.   <li class="ui-widget-content">Item 1</li>
  40.   <li class="ui-widget-content">Item 2</li>
  41.   <li class="ui-widget-content">Item 3</li>
  42.   <li class="ui-widget-content">Item 4</li>
  43.   <li class="ui-widget-content">Item 5</li>
  44.   <li class="ui-widget-content">Item 6</li>
  45. </ol>
  46.  
  47.  
  48. </body>
  49. </html>