簡體   English   中英

javascript:“ if”中的window.location.href

[英]javascript : window.location.href inside a “if”

我完全是用JavaScript編寫的newby,但遇到了問題。

一切都在這個“身體”中發生:

<body onKeyDown="myFunction(event);">

該代碼有效:當我在頁面上按Escape鍵時,會彈出一個27窗口。當我按另一個鍵時,什么也沒有發生。 這就是我想要的。

Key_ESC = 27;
function myFunction(event) {
    if ( event.keyCode == 27 ){
        alert(event.keyCode);
    }
}

此代碼有效:當我按任意鍵時,將轉到下一頁。 這就是我想要的。

Key_ESC = 27; 
function myFunction(event) {
        window.location.href=\"$INDEX/image`expr $compteur + 1`.html\";
}

但這是行不通的:當我按任意鍵時,什么也沒發生。 我認為按Escape鍵可以更改頁面(就像沒有“ if”一樣。事實並非如此。

Key_ESC = 27;
function myFunction(event) {
    if ( event.keyCode == 27 ){
        window.location.href=\"$INDEX/image`expr $compteur + 1`.html\";
    }
}

誰能向我解釋為什么“ window.location”在“ if”中不起作用? 我做錯了什么 ? 我真的一點都不知道。

非常感謝你。

您可能必須調用event.preventDefault

另外,在JS中只需執行以下操作:

window.onload = function ()
{

Key_ESC=27;
Key_LEFT=37;

document
.   event = 
    {   preventDefault: function( e )
        {   e.preventDefault?e.preventDefault():e.returnValue=false;
        }
    }
;
document.body
.   onkeyup = function nextPage ( e )
    {   
        switch
        (   e.keyCode
        )   {   case Key_ESC
                :   document.event.preventDefault(e);
                    window.location.href=URL_NEXT;
                case Key_LEFT
                :   document.event.preventDefault(e);
                    window.location.href=URL_PREV;
            } 
    }

}

一段時間我可以幫忙

function myFunction(event) {
    if (event.keyCode == 27){
        window.location.href="<%= request.getContextPath()%>/INDEX/imagename.html";
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM