要准确了解我在说什么,请查看此链接
所以,我想摆脱输入框,我的意思是在一个带有红色背景的 div 上输入一些东西,当我输入 google 并按 enter 时,我想将我发送到 google 网站,当我输入 yahoo 时发送我在雅虎网站上。我怎样才能做到这一点?我不是很擅长 javascript。谢谢!
要准确了解我在说什么,请查看此链接
所以,我想摆脱输入框,我的意思是在一个带有红色背景的 div 上输入一些东西,当我输入 google 并按 enter 时,我想将我发送到 google 网站,当我输入 yahoo 时发送我在雅虎网站上。我怎样才能做到这一点?我不是很擅长 javascript。谢谢!
这可能会有所帮助。
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('div[contenteditable]').focus();
$('div[contenteditable]').keypress(function (e) {
if(e.which == 13)
{
switch ($(this).html()) {
case 'google':
document.location = 'http://google.com'
break;
case 'yahoo':
document.location = 'http://yahoo.com'
break;
}
}
});
});
</script>
<style>
div[contenteditable] { background:red; width:200px;height:20px; padding:5px; }
</style>
</head>
<body>
<div contenteditable></div>
</body>
</html>
您还可以通过 css 隐藏您的输入,因此看起来您正在编写一个 div,但实际上它只是一个适当样式的输入。
像这样的东西?
您可以向您的页面添加一个具有 CONTENTEDITABLE="true" 属性的 div。比添加一个按钮或任何指示用户完成的东西(也可以是按返回键)。此时,检查 div 中的值,如果需要显示错误消息,或者如果输入的值有效则重定向页面(例如 document.location.href = 'http://www.' + userValue + '.com' ;)