在 JavaScript 中,在选择重置按钮后,如何将焦点设置在第一列?

huangapple go评论95阅读模式
英文:

How do I set focus on first column after selecting Reset button in javascript?

问题

I am working on a page that needs to be accessible through keyboard. Everything works fine but I am not sure how to set focus on the Name column (first dropdown) after I hit Filter Results button? I tried the following code but it doesn't work -

  1. $(".dropdown1").focus();

Is there a way to set focus on span id? or what's the best way to achieve this? Thanks.

英文:

I am working on a page that needs to be accessible through keyboard. Everything works fine but I am not sure how to set focus on the Name column (first dropdwon) after I hit Filter Results button? I tired the following code but it doesn't work -

  1. $(".dropdown1").focus();

Is there a way to set focus on spanid? or what's the best way to achieve this? Thanks.

Here is the link to my code - https://live.datatables.net/datubino/10/edit

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. $(document).ready(function() {
  2. var table = $(&#39;#example&#39;).DataTable({
  3. responsive: true,
  4. searching: true,
  5. columnDefs: [{
  6. target: 6,
  7. visible: false,
  8. searchable: true,
  9. }],
  10. dom: &#39;ilfrt&lt;&quot;left-col&quot;B&gt;&lt;&quot;toolbar&quot;&gt;&lt;&quot;right-col&quot;p&gt;&#39;,
  11. fnInitComplete: function() {
  12. $(&#39;div.toolbar&#39;).html(&#39;&lt;p&gt;Custom tool bar!&lt;/p&gt;&#39;);
  13. },
  14. buttons: [{
  15. extend: &#39;excelHtml5&#39;,
  16. className: &#39;mybutton&#39;,
  17. exportOptions: {
  18. columns: &#39;:visible&#39;
  19. }
  20. },
  21. // &#39;colvis&#39;
  22. ],
  23. });
  24. buildSelect(table);
  25. table.on(&#39;draw&#39;, function() {
  26. buildSelect(table);
  27. });
  28. $(&#39;#myInput&#39;).on(&#39;keyup&#39;, function() {
  29. table.search(this.value).draw();
  30. });
  31. $(&#39;#test&#39;).on(&#39;click&#39;, function() {
  32. table.search(&#39;&#39;).columns().search(&#39;&#39;).draw();
  33. $(&quot;.dropdown1&quot;).focus();
  34. });
  35. });
  36. function buildSelect(table) {
  37. var counter = 0;
  38. table.columns([0, 1, 2]).every(function() {
  39. var column = table.column(this, {
  40. search: &#39;applied&#39;
  41. });
  42. counter++;
  43. var select = $(&#39;&lt;select&gt;&lt;option value=&quot;&quot;&gt;select me&lt;/option&gt;&lt;/select&gt;&#39;)
  44. .appendTo($(&#39;#dropdown&#39; + counter).empty())
  45. .on(&#39;change&#39;, function() {
  46. var val = $.fn.dataTable.util.escapeRegex(
  47. $(this).val()
  48. );
  49. column
  50. .search(val ? &#39;^&#39; + val + &#39;$&#39; : &#39;&#39;, true, false)
  51. .draw();
  52. });
  53. if (column.index() === 0) {
  54. var filteredNames = column.data().unique().toArray();
  55. [&#39;Garrett&#39;, &#39;Test1&#39;, &#39;Ashton&#39;].forEach((d) =&gt; {
  56. if (filteredNames.includes(d)) {
  57. select.append(&#39;&lt;option value=&quot;&#39; + d + &#39;&quot;&gt;&#39; + d + &#39;&lt;/option&gt;&#39;);
  58. }
  59. });
  60. } else {
  61. column.data().unique().sort().each(function(d, j) {
  62. select.append(&#39;&lt;option value=&quot;&#39; + d + &#39;&quot;&gt;&#39; + d + &#39;&lt;/option&gt;&#39;);
  63. });
  64. }
  65. // The rebuild will clear the exisiting select, so it needs to be repopulated
  66. var currSearch = column.search();
  67. if (currSearch) {
  68. // ** MY CHANGE **
  69. // Use RegEx to find the selected value from the unique values of the column.
  70. // This will use the Regular Expression returned from column.search to find the first matching item in column.data().unique
  71. select.val(column.data().unique().toArray().find((e) =&gt; e.match(new RegExp(currSearch))));
  72. }
  73. });
  74. }

<!-- language: lang-html -->

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html&gt;
  3. &lt;head&gt;
  4. &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;https://cdn.datatables.net/1.10.15/css/jquery.dataTables.min.css&quot; /&gt;
  5. &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;https://cdn.datatables.net/buttons/1.4.0/css/buttons.dataTables.min.css&quot; /&gt;
  6. &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
  7. &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js&quot;&gt;&lt;/script&gt;
  8. &lt;script src=&quot;https://cdn.rawgit.com/bpampuch/pdfmake/0.1.27/build/pdfmake.min.js&quot;&gt;&lt;/script&gt;
  9. &lt;script src=&quot;https://cdn.rawgit.com/bpampuch/pdfmake/0.1.27/build/vfs_fonts.js&quot;&gt;&lt;/script&gt;
  10. &lt;script src=&quot;https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js&quot;&gt;&lt;/script&gt;
  11. &lt;script src=&quot;https://cdn.datatables.net/buttons/1.4.0/js/dataTables.buttons.min.js&quot;&gt;&lt;/script&gt;
  12. &lt;script src=&quot;https://cdn.datatables.net/buttons/1.4.0/js/buttons.flash.min.js&quot;&gt;&lt;/script&gt;
  13. &lt;script src=&quot;https://cdn.datatables.net/buttons/1.4.0/js/buttons.html5.min.js&quot;&gt;&lt;/script&gt;
  14. &lt;script src=&quot;https://cdn.datatables.net/buttons/1.4.0/js/buttons.print.min.js&quot;&gt;&lt;/script&gt;
  15. &lt;meta charset=utf-8 /&gt;
  16. &lt;title&gt;DataTables - JS Bin&lt;/title&gt;
  17. &lt;/head&gt;
  18. &lt;div class=&quot;searchbox&quot;&gt;
  19. &lt;p&gt;Name:
  20. &lt;span id=&quot;dropdown1&quot;&gt;
  21. &lt;/span&gt;
  22. &lt;/p&gt;
  23. &lt;p&gt;Postion: &lt;span id=&quot;dropdown2&quot;&gt;
  24. &lt;/span&gt;
  25. &lt;/p&gt;
  26. &lt;p&gt;Office: &lt;span id=&quot;dropdown3&quot;&gt;
  27. &lt;/span&gt;
  28. &lt;/p&gt;
  29. &lt;button type=&quot;button&quot; id=&quot;myInput&quot;&gt;Filter&lt;/button&gt;
  30. &lt;button type=&quot;button&quot; id=&quot;test&quot;&gt;Reset&lt;/button&gt;
  31. &lt;/div&gt;
  32. &lt;table id=&quot;example&quot; class=&quot;cell-border row-border stripe dataTable no-footer dtr-inline&quot; role=&quot;grid&quot; style=&quot; width: 100%; padding-top: 10px;&quot;&gt;
  33. &lt;thead&gt;
  34. &lt;tr&gt;
  35. &lt;th&gt;&amp;#160;&lt;/th&gt;
  36. &lt;th&gt;&amp;#160;&lt;/th&gt;
  37. &lt;th&gt;&amp;#160;&lt;/th&gt;
  38. &lt;th colspan=&quot;3&quot; style=&quot; text-align: center;&quot;&gt;Information&lt;/th&gt;
  39. &lt;th&gt;&amp;#160;&lt;/th&gt;
  40. &lt;/tr&gt;
  41. &lt;tr&gt;
  42. &lt;th&gt;Name&lt;/th&gt;
  43. &lt;th&gt;Position&lt;/th&gt;
  44. &lt;th&gt;Office&lt;/th&gt;
  45. &lt;th&gt;Age&lt;/th&gt;
  46. &lt;th&gt;Start date&lt;/th&gt;
  47. &lt;th&gt;Salary&lt;/th&gt;
  48. &lt;th&gt;Hidden Column&lt;/th&gt;
  49. &lt;/tr&gt;
  50. &lt;/thead&gt;
  51. &lt;tbody&gt;
  52. &lt;tr&gt;
  53. &lt;td&gt;Test1&lt;/td&gt;
  54. &lt;td&gt;test&lt;/td&gt;
  55. &lt;td&gt;test3&lt;/td&gt;
  56. &lt;td&gt;test4&lt;/td&gt;
  57. &lt;td&gt;2011/04/25&lt;/td&gt;
  58. &lt;td&gt;$3,120&lt;/td&gt;
  59. &lt;td&gt;Sys Architect&lt;/td&gt;
  60. &lt;/tr&gt;
  61. &lt;tr&gt;
  62. &lt;td&gt;Garrett&lt;/td&gt;
  63. &lt;td&gt;Director: fgghghjhjhjhkjkj&lt;/td&gt;
  64. &lt;td&gt;Edinburgh&lt;/td&gt;
  65. &lt;td&gt;63&lt;/td&gt;
  66. &lt;td&gt;2011/07/25&lt;/td&gt;
  67. &lt;td&gt;$5,300&lt;/td&gt;
  68. &lt;td&gt;Director:&lt;/td&gt;
  69. &lt;/tr&gt;
  70. &lt;tr&gt;
  71. &lt;td&gt;Ashton&lt;/td&gt;
  72. &lt;td&gt;Technical Authorjkjkjk fdfd h gjjjhjhk&lt;/td&gt;
  73. &lt;td&gt;San Francisco&lt;/td&gt;
  74. &lt;td&gt;66&lt;/td&gt;
  75. &lt;td&gt;2009/01/12&lt;/td&gt;
  76. &lt;td&gt;$4,800&lt;/td&gt;
  77. &lt;td&gt;Tech author&lt;/td&gt;
  78. &lt;/tr&gt;
  79. &lt;/tr&gt;
  80. &lt;/tbody&gt;
  81. &lt;/table&gt;
  82. &lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

使用#代替.来定位一个ID,并使用子组合器(&gt;)来获取&lt;span&gt;内部的&lt;select&gt;元素。

  1. $(&quot;#dropdown1 &gt; select&quot;).focus();
英文:

Use # instead of . to target an id and use the child combinator (&gt;) to get the &lt;select&gt; element inside that &lt;span&gt;.

  1. $(&quot;#dropdown1 &gt; select&quot;).focus();

huangapple
  • 本文由 发表于 2023年6月13日 06:28:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76460688.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定