将快捷键快捷方式添加到导航栏链接

时间:2020-08-10 07:45:08

标签: javascript html

我通过JavaScript在导航栏上添加了4个链接。如何通过在供稿<iframe>中按1-4键来加载这些链接?

<li>
   <script type="text/javascript">
    document.write('<a href="https://linkhere.com/blah.html?'+new Date().getTime()+'" target="feeds">1</a>');
</script>
</li>
<iframe name="feeds" width="50%" height="500"></iframe>

1 个答案:

答案 0 :(得分:0)

查看此代码:

document.addEventListener('keypress', function (event) {
        if (event.key === '1')
            document.getElementsByName('feeds')[0].src = 
        'https://linkhere.com/blah.html?'+new Date().getTime();
    });
<iframe name="feeds" width="50%" height="500"></iframe>

在这里,我们正在为键“ 1”设置键盘快捷键。您可以用类似的方式为其他键添加键盘快捷键。