如何在按下某个键时使用 JavaScript/HTML/CSS 加载新页面?
How can I use JavaScript/HTML/CSS to load a new page when a key is pressed down?
用户按下某个键后移动到新屏幕的代码是什么。例如,我编写了一个空白屏幕,上面写着按任意键开始。
我在网上找了半天,没找到不涉及jQuery等的代码
提前致谢。
<script>
//created a variable for instructions
var instructions = '<h1><center>You will see three different symbols appear</center></h1>';
//Created a button
var button = document.createElement('button');
button.innerHTML = 'Click here to Start';
var body = document.getElementsByTagName('body')[0];
body.appendChild(button);
button.addEventListener('click', function(){
if('click'){
document.open(newPage)
}
});
//Pressing a key (space bar in this case) will open up a blank page with the instructions of the experiment
document.addEventListener('keydown',function(e){
if(e.keyCode==32){
document.location.href=('about:blank');
document.write(instructions)
}
});
</script>
尝试
document.onkeydown= e => location='https://example.com'
Click here and push some key
最好使用EventListener
document.addEventListener('keydown',function(e){
if(e.keyCode==65)//key code for 'a'
location="http://google.com"
})
关键代码列表:http://gcctech.org/csc/javascript/javascript_keycodes.htm
用户按下某个键后移动到新屏幕的代码是什么。例如,我编写了一个空白屏幕,上面写着按任意键开始。
我在网上找了半天,没找到不涉及jQuery等的代码
提前致谢。
<script>
//created a variable for instructions
var instructions = '<h1><center>You will see three different symbols appear</center></h1>';
//Created a button
var button = document.createElement('button');
button.innerHTML = 'Click here to Start';
var body = document.getElementsByTagName('body')[0];
body.appendChild(button);
button.addEventListener('click', function(){
if('click'){
document.open(newPage)
}
});
//Pressing a key (space bar in this case) will open up a blank page with the instructions of the experiment
document.addEventListener('keydown',function(e){
if(e.keyCode==32){
document.location.href=('about:blank');
document.write(instructions)
}
});
</script>
尝试
document.onkeydown= e => location='https://example.com'
Click here and push some key
最好使用EventListener
document.addEventListener('keydown',function(e){
if(e.keyCode==65)//key code for 'a'
location="http://google.com"
})
关键代码列表:http://gcctech.org/csc/javascript/javascript_keycodes.htm