【问题标题】:'Autofocus' attribute works once, but not twice, on input elements'Autofocus' 属性在输入元素上工作一次,但不是两次
【发布时间】:2018-09-10 15:53:33
【问题描述】:

我在 d3.js 中编码,有时使用 input 元素来收集用户的输入,而不使用 form。这很好用。

但我注意到我无法在动态页面上始终如一地应用 autofocus 属性;它仅在第一次应用时有效。

下面是一个示例,其中包含所有编码。请注意autofocus 第一次出现input 元素时如何完美运行。即使编码相同,第二次也不行。

解决此问题将改善智能手机的用户体验。

注意:.focus() 方法不起作用,我认为这是因为它只能应用于表单中的input 元素。

h1,
p,
input {
  font-family: sans-serif;
}

h1 {
  font-size: 1rem;
}

input {
  padding: 0.5rem;
  border-radius: 1rem;
  color: gray;
  opacity: 0;
}

input::placeholder {
  font-weight: normal;
  color: silver;
}

input:focus {
  outline: none;
}
<script src="https://d3js.org/d3.v4.min.js"></script>

<div id="container">
  <h1>Testing the autofocus</h1>
</div>

<script>
  // wait 2 seconds, then create input element with autofocus
  d3.timeout(function() {
    createFirstInput();
  }, 2000);

  function createFirstInput() {
    d3.select("h1")
      .text("Creating first input field")
    d3.select("#container")
      .append("input")
      .attr("type", "text")
      .attr("placeholder", "input goes here")
      .attr("autofocus", "autofocus")
      .transition()
      .duration(2000)
      .style("opacity", 1);
    d3.select("#container")
      .append("p")
      .text("autofocus works; cursor appears in input field");
    // delete input field after 5 seconds
    d3.timeout(function() {
      d3.select("h1")
        .text("Creating second input field");
      d3.selectAll("input, p")
        .remove();
      // create second input field
      createSecondInput();
    }, 5000);
  }

  function createSecondInput() {
    // wait 2 seconds, then create input element with autofocus
    d3.timeout(function() {
      d3.select("#container")
        .append("input")
        .attr("type", "text")
        .attr("placeholder", "input goes here")
        .attr("autofocus", "autofocus")
        .transition()
        .duration(2000)
        .style("opacity", 1);
      d3.select("#container")
        .append("p")
        .text("autofocus doesn't work; no cursor in input field");
    }, 2000);
  }
</script>

【问题讨论】:

    标签: javascript html forms d3.js focus


    【解决方案1】:

    focus() 方法确实有效:

    input.node().focus();
    

    这里,input 是附加输入的 D3 选择(顺便说一下,命名您的选择!这是一个很好的做法)。通过在该选择上使用node(),我们得到了实际的DOM元素,我们使用focus()方法。

    这里是演示:

    h1,
    p,
    input {
      font-family: sans-serif;
    }
    
    h1 {
      font-size: 1rem;
    }
    
    input {
      padding: 0.5rem;
      border-radius: 1rem;
      color: gray;
      opacity: 0;
    }
    
    input::placeholder {
      font-weight: normal;
      color: silver;
    }
    
    input:focus {
      outline: none;
    }
    <script src="https://d3js.org/d3.v4.min.js"></script>
    
    <div id="container">
      <h1>Testing the autofocus</h1>
    </div>
    
    <script>
      // wait 2 seconds, then create input element with autofocus
      d3.timeout(function() {
        createFirstInput();
      }, 2000);
    
      function createFirstInput() {
        d3.select("h1")
          .text("Creating first input field")
        d3.select("#container")
          .append("input")
          .attr("type", "text")
          .attr("placeholder", "input goes here")
          .attr("autofocus", "autofocus")
          .transition()
          .duration(2000)
          .style("opacity", 1);
        d3.select("#container")
          .append("p")
          .text("autofocus works; cursor appears in input field");
        // delete input field after 5 seconds
        d3.timeout(function() {
          d3.select("h1")
            .text("Creating second input field");
          d3.selectAll("input, p")
            .remove();
          // create second input field
          createSecondInput();
        }, 5000);
      }
    
      function createSecondInput() {
        // wait 2 seconds, then create input element with autofocus
        d3.timeout(function() {
          var input = d3.select("#container")
            .append("input")
            .attr("type", "text")
            .attr("placeholder", "input goes here")
            .attr("autofocus", "autofocus")
            .transition()
            .duration(2000)
            .style("opacity", 1);
          input.node().focus();
          d3.select("#container")
            .append("p")
            .text("autofocus with focus() method does work");
        }, 2000);
      }
    </script>

    【讨论】:

    • 乐于助人,杰拉尔多!我没有意识到我必须将.focus() 方法应用于.node()。即使是documentation 和大多数网络示例,也建议我可以将其直接应用于input 元素。这比autofocus高效得多。
    • @Markus 您只能将其应用于输入元素。这正是我在这里使用node() 的原因,否则你有一个 D3 选择,而不是 DOM 元素。
    【解决方案2】:

    这是因为自动对焦应该只提供给一个输入元素。如果要更改焦点,请使用 node() 的 focus() 方法 这是代码。

    <div id="container">
      <h1>Testing the autofocus</h1>
    </div>
    
    <script>
      // wait 2 seconds, then create input element with autofocus
      d3.timeout(function() {
        createFirstInput();
      }, 2000);
    
      function createFirstInput() {
        d3.select("h1")
          .text("Creating first input field")
        d3.select("#container")
          .append("input")
          .attr("type", "text")
          .attr("placeholder", "input goes here")
          .transition()
          .duration(2000)
          .style("opacity", 1)
          .node()
          .focus();
        d3.select("#container")
          .append("p")
          .text("autofocus works; cursor appears in input field");
        // delete input field after 5 seconds
        d3.timeout(function() {
          d3.select("h1")
            .text("Creating second input field");
          d3.selectAll("input, p")
            .remove();
          // create second input field
          createSecondInput();
        }, 5000);
      }
    
      function createSecondInput() {
        // wait 2 seconds, then create input element with autofocus
        d3.timeout(function() {
          var input = d3.select("#container")
            .append("input")
            .attr("type", "text")
            .attr("placeholder", "input goes here")
            .transition()
            .duration(2000)
            .style("opacity", 1)
            .node()
            .focus();
          d3.select("#container")
            .append("p")
            .text("autofocus should be given to only one input element. If you want to change the focus use focus() method of node()");
        }, 2000);
      }
    </script>
    

    【讨论】:

    • 谢谢萨米尔;感谢有关使用.node() 的提醒(我不知道需要它)。但是,我想知道 为什么 autofocus 不起作用。我一次只将它应用于一个元素,并且在将autofocus 应用于新元素之前删除了该元素。
    猜你喜欢
    • 2017-06-09
    • 1970-01-01
    • 1970-01-01
    • 2013-02-09
    • 2021-10-20
    • 2020-04-22
    • 1970-01-01
    • 1970-01-01
    • 2015-10-29
    相关资源
    最近更新 更多