如何使用键盘上的箭头键来触发博客内的导航(上一页/下一页)链接
到目前为止,我拼凑在一起的脚本如下所示:
<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 的,但通常也是这样:
有没有办法让我的左右箭头键触发这些动态链接?
感谢您的阅读,非常感谢您对此提供的任何帮助。
the script i've pieced together so far looks like this:
<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>
and my html looks like this:
<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>
the {PreviousPage} / {NextPage} code represents dynamic page links which are different depending on which page you are on. this particular question is specific to tumblr, but generally as well:
is there a way to get my left and right arrow keys to trigger these dynamic links?
thank you for reading and any help with this is greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用它来告诉您
对象的
keyIdentifier
属性。然后,您可以使用 if-then 逻辑来忽略您不感兴趣的所有按键,并将正确的行为连接到您感兴趣的按键。
下面将向您的链接分配左右箭头键(基于锚点/链接元素的 id)。
Use this to tell you the
keyIdentifier
attribute of theobject.
Then you can use if-then logic to ignore all key presses you aren't interested in, and wire the correct behavior to the ones you are.
The following will assign the left and right arrow keys to your links (based on the id of the anchor/link elements).