简体   繁体   English

添加锚点和onClick到链接

[英]add an anchor and onClick to a link

I have an image with a link that when clicked I would like the user to be brought to an anchor on the same page and then the cursor to be focused on the input directly below. 我有一个带有链接的图像,当单击该链接时,我希望将用户带到同一页面上的锚点,然后将光标聚焦在正下方的输入上。

<a onclick="document.newsletter.email.focus();" href="#newssection"><img src="images/newsletter_slide.jpg" /></a>

When it is just the onClick it works but does not scroll the page down and when I use the anchor it goes to that section but does not focus. 当它只是onClick时,它可以工作,但不会向下滚动页面;当我使用锚点时,它会转到该部分,但不会聚焦。

I switched around the onClick and the href and it didnt help 我切换了onClick和href,但并没有帮助

Try this. 尝试这个。

http://jsfiddle.net/HgSjH/3/ http://jsfiddle.net/HgSjH/3/

<a onclick="document.newsletter.email.focus();return false;" href="#">click me</a>
<form name="newsletter" id="newssection">
  <input id="email" name="email"/>
</form>

I think a simple 我认为一个简单

document.getElementById('yourEmailField').focus(); should do the trick. 应该可以。

It will automatically focus to the element, and if needed scroll too. 它将自动聚焦到该元素,并且如果需要也滚动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM