在下面的示例代碼中,我有一個按鈕,當點擊重新加載頁面,但按Enter鍵不這樣做,如何修改它,以便點擊輸入也會刷新頁面Javascript - 事件,重新加載頁面
<h3>click to refresh page</h3>
<input type="button" value="Refresh" onClick="history.go(0)">
謝謝 露絲
在下面的示例代碼中,我有一個按鈕,當點擊重新加載頁面,但按Enter鍵不這樣做,如何修改它,以便點擊輸入也會刷新頁面Javascript - 事件,重新加載頁面
<h3>click to refresh page</h3>
<input type="button" value="Refresh" onClick="history.go(0)">
謝謝 露絲
如果你願意,你可以在頁面上設置一個按鍵處理程序趕上任何 ENTER按鍵的任何地方:
function catchCR(e) {
if (!e) {
e = window.event; // for IE
}
var key = 0;
if (e.keyCode) { key = e.keyCode; } // IE
if (e.which) { key = e.which; } // FF
if (key == 13 /* enter key */) {
history.go(0);
}
}
if (document.addEventListener) {
document.addEventListener("keydown", catchCR, true);
} else if (document.attachEvent) {
document.attachEvent("onkeydown",catchCR);
}
您需要設置焦點。在你的onload功能:
var button = document.getElementById("mybutton");
button.focus();
您可能還對按鍵事件添加:
http://help.dottoro.com/ljlwfxum.php
http://www.devguru.com/Technologies/ecmascript/quickref/evhan_onkeypress.html
你可以這樣說:
<h3>click to refresh page</h3>
<form onSubmit="history.go(0); return false;">
<input type="submit" value="Search"></form>
您可以通過放置在原稿設置的事件做。
試試這個:
//Traditional Way
document.onkeypress = function(event){
event = event || window.event;
key=event.keyCode;
if (key == "13") //Enter
{
history.go(0);
}
};
//OR
function keyHandler(event){
event = event || window.event;
key=event.keyCode;
if (key == "13") //Enter
{
history.go(0);
}
}
//Modern W3c Way
if (document.addEventListener) {
document.addEventListener("keydown", keyHandler, false);
}
//IE Way
else if (document.attachEvent) {
document.attachEvent("onkeydown",keyHandler);
}
它看起來像你正在建設一個魯布·戈德堡機械...不要點擊一個來自Javascript的按鈕!請直接調用你想要的行爲 – 2010-08-13 13:37:26
@ Shiny and New。先生,冷靜下來!我已經習慣了爲其他操作做這件事,但我想這裏不需要它。 – 2010-08-13 13:41:58
我不能讓這種方法在Firefox 3.6中 – 2010-08-13 13:30:39
爲我工作的錯誤認識:( – puckipedia 2010-08-14 09:26:00