Navigation by specific inputted text in text box?
first post. I am attempting to create a text "input" box开发者_如何学C that will allow any of three commands "who", "why" and "how" to link to corresponding pages in my website. To clarify, I want my homepage to have a text box that will allow the user to type one of the three above commands then press "enter" to link to the corresponding page. Sounds easy, but I am a super beginner. Any help would be great thanks.
You would add a keydown listener to the input box in JavaScript and listen for keycode 13 (enter button keycode). If it is pressed, test the value for who, why, and how.
<input type="text" id="LinkBox" />
document.getElementById("LinkBox").onkeydown = function(e) {
var keyCode = e.keyCode || e.which;
// 13 is keycode for the enter key
if ( keyCode === 13 ) {
if ( this.value === "who" ) {
window.location = // who URL here
} else if ( this.value === "why" ) {
window.location = // why URL here
} else if ( this.value === "how" ) {
window.location = // how URL here
}
}
}
精彩评论