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

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

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 -

$(".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 -

$(".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 -->

$(document).ready(function() {
  var table = $(&#39;#example&#39;).DataTable({
    responsive: true,
    searching: true,
    columnDefs: [{
      target: 6,
      visible: false,
      searchable: true,
    }],
    dom: &#39;ilfrt&lt;&quot;left-col&quot;B&gt;&lt;&quot;toolbar&quot;&gt;&lt;&quot;right-col&quot;p&gt;&#39;,
    fnInitComplete: function() {
      $(&#39;div.toolbar&#39;).html(&#39;&lt;p&gt;Custom tool bar!&lt;/p&gt;&#39;);
    },
    buttons: [{
        extend: &#39;excelHtml5&#39;,
        className: &#39;mybutton&#39;,
        exportOptions: {
          columns: &#39;:visible&#39;
        }
      },
     //  &#39;colvis&#39;
    ],
  });
  buildSelect(table);
  table.on(&#39;draw&#39;, function() {
    buildSelect(table);
  });

  $(&#39;#myInput&#39;).on(&#39;keyup&#39;, function() {
    table.search(this.value).draw();
  });

  $(&#39;#test&#39;).on(&#39;click&#39;, function() {
    table.search(&#39;&#39;).columns().search(&#39;&#39;).draw();
    $(&quot;.dropdown1&quot;).focus();
  });
});

function buildSelect(table) {
  var counter = 0;
  table.columns([0, 1, 2]).every(function() {
    var column = table.column(this, {
      search: &#39;applied&#39;
    });
    counter++;
    var select = $(&#39;&lt;select&gt;&lt;option value=&quot;&quot;&gt;select me&lt;/option&gt;&lt;/select&gt;&#39;)
      .appendTo($(&#39;#dropdown&#39; + counter).empty())
      .on(&#39;change&#39;, function() {
        var val = $.fn.dataTable.util.escapeRegex(
          $(this).val()
        );

        column
          .search(val ? &#39;^&#39; + val + &#39;$&#39; : &#39;&#39;, true, false)
          .draw();
      });

    if (column.index() === 0) {
      var filteredNames = column.data().unique().toArray();
      [&#39;Garrett&#39;, &#39;Test1&#39;, &#39;Ashton&#39;].forEach((d) =&gt; {
        if (filteredNames.includes(d)) {
          select.append(&#39;&lt;option value=&quot;&#39; + d + &#39;&quot;&gt;&#39; + d + &#39;&lt;/option&gt;&#39;);
        }
      });
    } else {
      column.data().unique().sort().each(function(d, j) {
        select.append(&#39;&lt;option value=&quot;&#39; + d + &#39;&quot;&gt;&#39; + d + &#39;&lt;/option&gt;&#39;);
      });
    }

    // The rebuild will clear the exisiting select, so it needs to be repopulated
    var currSearch = column.search();
    if (currSearch) {
      // ** MY CHANGE **
      // Use RegEx to find the selected value from the unique values of the column.
      // This will use the Regular Expression returned from column.search to find the first matching item in column.data().unique
      select.val(column.data().unique().toArray().find((e) =&gt; e.match(new RegExp(currSearch))));
    }
  });
}

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

&lt;!DOCTYPE html&gt;
&lt;html&gt;

&lt;head&gt;
  &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;
  &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;

  &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;https://cdn.rawgit.com/bpampuch/pdfmake/0.1.27/build/pdfmake.min.js&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;https://cdn.rawgit.com/bpampuch/pdfmake/0.1.27/build/vfs_fonts.js&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;https://cdn.datatables.net/buttons/1.4.0/js/dataTables.buttons.min.js&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;https://cdn.datatables.net/buttons/1.4.0/js/buttons.flash.min.js&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;https://cdn.datatables.net/buttons/1.4.0/js/buttons.html5.min.js&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;https://cdn.datatables.net/buttons/1.4.0/js/buttons.print.min.js&quot;&gt;&lt;/script&gt;
  &lt;meta charset=utf-8 /&gt;
  &lt;title&gt;DataTables - JS Bin&lt;/title&gt;
&lt;/head&gt;
&lt;div class=&quot;searchbox&quot;&gt;
  &lt;p&gt;Name:
    &lt;span id=&quot;dropdown1&quot;&gt;
  &lt;/span&gt;
  &lt;/p&gt;
  &lt;p&gt;Postion: &lt;span id=&quot;dropdown2&quot;&gt;
  &lt;/span&gt;
  &lt;/p&gt;
  &lt;p&gt;Office: &lt;span id=&quot;dropdown3&quot;&gt;
&lt;/span&gt;
  &lt;/p&gt;
  &lt;button type=&quot;button&quot; id=&quot;myInput&quot;&gt;Filter&lt;/button&gt;
  &lt;button type=&quot;button&quot; id=&quot;test&quot;&gt;Reset&lt;/button&gt;
&lt;/div&gt;
&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;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;&amp;#160;&lt;/th&gt;
      &lt;th&gt;&amp;#160;&lt;/th&gt;
      &lt;th&gt;&amp;#160;&lt;/th&gt;
      &lt;th colspan=&quot;3&quot; style=&quot; text-align: center;&quot;&gt;Information&lt;/th&gt;
      &lt;th&gt;&amp;#160;&lt;/th&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;Name&lt;/th&gt;
      &lt;th&gt;Position&lt;/th&gt;
      &lt;th&gt;Office&lt;/th&gt;
      &lt;th&gt;Age&lt;/th&gt;
      &lt;th&gt;Start date&lt;/th&gt;
      &lt;th&gt;Salary&lt;/th&gt;
      &lt;th&gt;Hidden Column&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Test1&lt;/td&gt;
      &lt;td&gt;test&lt;/td&gt;
      &lt;td&gt;test3&lt;/td&gt;
      &lt;td&gt;test4&lt;/td&gt;
      &lt;td&gt;2011/04/25&lt;/td&gt;
      &lt;td&gt;$3,120&lt;/td&gt;
      &lt;td&gt;Sys Architect&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Garrett&lt;/td&gt;
      &lt;td&gt;Director: fgghghjhjhjhkjkj&lt;/td&gt;
      &lt;td&gt;Edinburgh&lt;/td&gt;
      &lt;td&gt;63&lt;/td&gt;
      &lt;td&gt;2011/07/25&lt;/td&gt;
      &lt;td&gt;$5,300&lt;/td&gt;
      &lt;td&gt;Director:&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Ashton&lt;/td&gt;
      &lt;td&gt;Technical Authorjkjkjk fdfd h gjjjhjhk&lt;/td&gt;
      &lt;td&gt;San Francisco&lt;/td&gt;
      &lt;td&gt;66&lt;/td&gt;
      &lt;td&gt;2009/01/12&lt;/td&gt;
      &lt;td&gt;$4,800&lt;/td&gt;
      &lt;td&gt;Tech author&lt;/td&gt;
    &lt;/tr&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

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

$(&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;.

$(&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:

确定