课程表

jQuery UI 基础

jQuery UI 主题

jQuery UI 部件库

jQuery UI 参考手册

jQuery UI 实例

工具箱
速查手册

选择(Selectable)

当前位置:免费教程 » JS/JS库/框架 » jQuery UI

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

如需了解更多有关 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. <style>
  11. #feedback { font-size: 1.4em; }
  12. #selectable .ui-selecting { background: #FECA40; }
  13. #selectable .ui-selected { background: #F39814; color: white; }
  14. #selectable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
  15. #selectable li { margin: 3px; padding: 0.4em; font-size: 1.4em; height: 18px; }
  16. </style>
  17. <script>
  18. $(function() {
  19. $( "#selectable" ).selectable();
  20. });
  21. </script>
  22. </head>
  23. <body>
  24. <ol id="selectable">
  25. <li class="ui-widget-content">Item 1</li>
  26. <li class="ui-widget-content">Item 2</li>
  27. <li class="ui-widget-content">Item 3</li>
  28. <li class="ui-widget-content">Item 4</li>
  29. <li class="ui-widget-content">Item 5</li>
  30. <li class="ui-widget-content">Item 6</li>
  31. <li class="ui-widget-content">Item 7</li>
  32. </ol>
  33. </body>
  34. </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. <style>
  11. #feedback { font-size: 1.4em; }
  12. #selectable .ui-selecting { background: #FECA40; }
  13. #selectable .ui-selected { background: #F39814; color: white; }
  14. #selectable { list-style-type: none; margin: 0; padding: 0; width: 450px; }
  15. #selectable li { margin: 3px; padding: 1px; float: left; width: 100px; height: 80px; font-size: 4em; text-align: center; }
  16. </style>
  17. <script>
  18. $(function() {
  19. $( "#selectable" ).selectable();
  20. });
  21. </script>
  22. </head>
  23. <body>
  24. <ol id="selectable">
  25. <li class="ui-state-default">1</li>
  26. <li class="ui-state-default">2</li>
  27. <li class="ui-state-default">3</li>
  28. <li class="ui-state-default">4</li>
  29. <li class="ui-state-default">5</li>
  30. <li class="ui-state-default">6</li>
  31. <li class="ui-state-default">7</li>
  32. <li class="ui-state-default">8</li>
  33. <li class="ui-state-default">9</li>
  34. <li class="ui-state-default">10</li>
  35. <li class="ui-state-default">11</li>
  36. <li class="ui-state-default">12</li>
  37. </ol>
  38. </body>
  39. </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. <style>
  11. #feedback { font-size: 1.4em; }
  12. #selectable .ui-selecting { background: #FECA40; }
  13. #selectable .ui-selected { background: #F39814; color: white; }
  14. #selectable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
  15. #selectable li { margin: 3px; padding: 0.4em; font-size: 1.4em; height: 18px; }
  16. </style>
  17. <script>
  18. $(function() {
  19. $( "#selectable" ).selectable({
  20. stop: function() {
  21. var result = $( "#select-result" ).empty();
  22. $( ".ui-selected", this ).each(function() {
  23. var index = $( "#selectable li" ).index( this );
  24. result.append( " #" + ( index + 1 ) );
  25. });
  26. }
  27. });
  28. });
  29. </script>
  30. </head>
  31. <body>
  32. <p id="feedback">
  33. <span>您已经选择了:</span> <span id="select-result"></span>
  34. </p>
  35. <ol id="selectable">
  36. <li class="ui-widget-content">Item 1</li>
  37. <li class="ui-widget-content">Item 2</li>
  38. <li class="ui-widget-content">Item 3</li>
  39. <li class="ui-widget-content">Item 4</li>
  40. <li class="ui-widget-content">Item 5</li>
  41. <li class="ui-widget-content">Item 6</li>
  42. </ol>
  43. </body>
  44. </html>

我要试一下

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号