簡體   English   中英

如何獲取鍵盤上的箭頭鍵以觸發博客中的導航(上一頁/下一頁)鏈接

[英]how to get the arrow keys on the keyboard to trigger navigation (previous/next page) links within a blog

我到目前為止拼湊在一起的腳本看起來像這樣:

<script type="text/javascript">
/* KEYNAV */
document.onkeydown = function(e) {
if (! e) var e = window.event;
var code = e.charCode ? e.charCode : e.keyCode;
if (! e.shiftKey && ! e.ctrlKey && ! e.altKey && ! e.metaKey) {
if (code == Event.KEY_LEFT) {
if ($('previous_page_link')) location.href = $('previous_page_link').href;
} else if (code == Event.KEY_RIGHT) {
if ($('next_page_link')) location.href = $('next_page_link').href;}
}
}); 
</script>

我的HTML看起來像這樣:

<p>
{block:PreviousPage}
<a id="previous_page_link" href="{PreviousPage}">PREVIOUS PAGE</a> 
{/block:PreviousPage}

{block:NextPage}
<a id="next_page_link" href="{NextPage}">NEXT PAGE</a>
{/block:NextPage}
</p>

{PreviousPage} / {NextPage}代碼表示動態頁面鏈接,這取決於您所在的頁面。 這個特殊問題特定於tumblr,但通常也是如此:

有沒有辦法讓我的左右箭頭鍵觸發這些動態鏈接?

感謝您的閱讀和任何幫助,非常感謝。

function leftArrowPressed() {
   // Your stuff here
}

function rightArrowPressed() {
   // Your stuff here
}

document.onkeydown = function(evt) {
    evt = evt || window.event;
    switch (evt.keyCode) {
        case 37:
            leftArrowPressed();
            break;
        case 39:
            rightArrowPressed();
            break;
    }
};

用它來告訴你keyIdentifier屬性 賓語。

<html>
<head>

<script type="text/javascript">
  document.onkeydown = function() {
  alert (event.keyIdentifier);
}; 
</script>
</head>
<body>
</body>
</html>

然后,您可以使用if-then邏輯忽略您不感興趣的所有按鍵,並將正確的行為連接到您的行為。

以下內容將左右箭頭鍵分配給您的鏈接(基於錨點/鏈接元素的ID)。

<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>

<script type="text/javascript">
    $(document).ready(function() {
            document.onkeydown = function() 
                {
                    var j = event.keyIdentifier
                    if (j == "Right")
                        window.location = nextUrl
                    else if (j == "Left")
                        window.location = prevUrl            
                        }
                   });

      $(document).ready(function() {
                    var nextPage = $("#next_page_link")
                    var prevPage = $("#previous_page_link")
                    nextUrl = nextPage.attr("href")
                    prevUrl = prevPage.attr("href")
                });

</script>
</head>
<body>
<p>
    <a id="previous_page_link" href="http://www.google.com">Google</a> 
    <a id="next_page_link" href="http://www.yahoo.com">Yahoo</a>
</p>
</body>
</html>

暫無
暫無

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

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