要准确理解我在说什么,请查看此 link
所以,我想摆脱输入框,我的意思是在红色背景的 div 上输入一些内容,例如,当我输入 google 并按 Enter 键时,我想将我发送到 google 网站,当我输入时雅虎向我发送雅虎网站。 我怎样才能做到这一点?我不太擅长 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>
关于javascript - jquery 实时打字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7647554/