如何临时设置所有元素的tabindex属性,并能够将所有元素重置为其原始tabindex?

时间:2018-01-15 11:58:58

标签: javascript jquery html

如果每个元素都有自己的tabindex,如下所示:

<a href="http://example.com" tabindex="3">Link</a>
<input type="text" tabindex="2">
<select tabindex="5">
    <option>Option A</option>
    <option>Option B</option>
    <option>Option C</option>
</select>
<textarea tabindex="4">Hello world</textarea>
<button tabindex="1" id="Btn">Button</button>

现在,我向button[id=Btn]添加了一个事件,该事件将所有元素“tabindex设置为-1,如下所示:

document.getElementById("Btn").addEventListener("click", function(){
    var allElement = document.querySelectorAll("a,area,button,input,object,select,textarea");
    for(var i = 0; i < allElement.length; i++){
        allElement[i].setAttribute("tabindex",-1);
    }
});

点击button[id=Btn]后,如何将所有元素重置为原始tabindex

1 个答案:

答案 0 :(得分:3)

将当前function extractParameters(args) { function getCase(arg, key) { return arg.match(new RegExp(`${key}=(.*)`)) || {}; } args.forEach((arg) => { console.log("arg: " + arg); let match; switch (arg) { case (match = getCase(arg, "--user")).input: case (match = getCase(arg, "-u")).input: userName = match[1]; break; case (match = getCase(arg, "--password")).input: case (match = getCase(arg, "-p")).input: password = match[1]; break; case (match = getCase(arg, "--branch")).input: case (match = getCase(arg, "-b")).input: branch = match[1]; break; } }); }; 保存在tabindex属性中,并在需要时使用它恢复原始tabindex值:

&#13;
&#13;
data-
&#13;
var allElement = document.querySelectorAll("a,area,button,input,object,select,textarea");

document.getElementById("Btn").addEventListener("click", function() {
  for (var i = 0; i < allElement.length; i++) {
    allElement[i].setAttribute("data-tabindex", allElement[i].getAttribute("tabindex"));
    allElement[i].setAttribute("tabindex", -1);
  }
});

document.getElementById("BtnRes").addEventListener("click", function() {
  for (var i = 0; i < allElement.length; i++) {
    allElement[i].setAttribute("tabindex", allElement[i].getAttribute("data-tabindex"));
    allElement[i].removeAttribute("data-tabindex", -1);
  }
});
&#13;
&#13;
&#13;