admin 管理员组文章数量: 1086019
Using html (and possibly javascript = but I would prefer not) , I would like to jump from a link in one of my pages to a textbox somewhere near the button, focusing on that section. How can I do that?
Using html (and possibly javascript = but I would prefer not) , I would like to jump from a link in one of my pages to a textbox somewhere near the button, focusing on that section. How can I do that?
Share Improve this question asked Jan 15, 2012 at 19:21 YuvalYuval 5147 silver badges17 bronze badges4 Answers
Reset to default 3Given an input that looks like:
<input id="myInput" type="text" value="" />
You could make your link automatically jump to that textbox by making the href attribute point to a hash url:
<a id="myLink" href="#myInput">Show me the textbox!</a>
And wire in javascript(with jQuery in this example) to focus on the textbox when the link is clicked:
$(document).ready(function() {
$("#myLink").click(function(){
$("#myInput").focus();
});
});
You want to focus on a textbox on click of an element?
var d = document;
d.getElementById("myLink").onclick = function() {
d.getElementById("myTextInput").focus();
};
Demo.
Assuming your textbox has an id
attribute, your link can point to its id with a hash, and the onclick
can focus it:
<a href='#textboxid' onclick='document.getElementById("textboxid").focus();'>Click me</a>
When the link is clicked, the page will jump to the textbox's position by the #textboxid
hash, and its focus()
method is called to focus it.
<a href="#myinput">Input Link</a>
<input type="text" value="Text Input" id="myinput" />
With HTML using the above code, you'll only be able to jump to the input field.
本文标签: javascriptHow can I make a link direct to an input fieldStack Overflow
版权声明:本文标题:javascript - How can I make a link direct to an input field? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1743999103a2516117.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论