在href之后给予焦点

时间:2016-02-16 15:20:25

标签: html css focus

我想知道是否有办法将焦点放在本地input之后的href

就像这里我所拥有的:

<a href="#bottom"> Sign up </a>
And at the bottom of the page :
<div id="bottom">
<input type="email" name="email" id="email" placeholder="email address">
</div>

我想要的是当有人在注册时出现问题时,它直接进入底部页面并将注意力集中在input上,这样用户就可以直接输入他的电子邮件地址,而不必使用clic。

谢谢!

2 个答案:

答案 0 :(得分:3)

使用label样式a。确保label的{​​{1}}属性与for的{​​{1}}属性相匹配。这会在点击input时关注id并滚动页面,以便查看input

label
input

答案 1 :(得分:1)

这应该可以解决问题:

已更新 HTML:在链接中添加了ID

<a id="signUpLink" href="#bottom"> Sign up </a>
And at the bottom of the page :
<div id="bottom">
<input type="email" name="email" id="email" placeholder="email address">
</div>

<强>的jQuery / JavaScript的

$(document).ready(function(){
  var $link = $("#signUpLink");
  var $emailInput = $("#email");

  $link.on("click", function(){
      $emailInput.focus();
  });
});

这是小提琴https://jsfiddle.net/yfs6nxmb/1/