将键盘元素添加到按钮

我在这里有一个名为的按钮.togglePL,并对其进行了编码以展开或折叠 中的页面选择器#pageList。我想使用键盘元素展开或折叠页面选择器,而alt + p无需单击按钮本身。我尝试过研究如何通过在线 JS 和此处的问题来做到这一点,但没有成功。请查找随附的代码,任何帮助将不胜感激。谢谢。


// Toggle Page List

$(document).ready(function() {

    $('.togglePL').click(function(e) {

        if ($('#pageList').width() == 40) {

            $('.textPL').show();

            $('#pageList').width(168);

            $('.togglePL').css({left:'192px', transform: 'none'});

             $('#pageList a').css({fontSize:'10pt'});

        }

        else {

            $('.textPL').hide();

            $('#pageList').width(40);

            $('.togglePL').css({left:'64px', transform: 'rotate(180deg)', transitionDuration:'0.3s'});

            $('#pageList a').css({fontSize:'14pt'});

        }

    });

});

<!doctype html>

<!--

    ~ Copyright (c) Summit Learning Management System (made by students, for students). 2020.

-->

<html><head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0">

    <title>Welcome - Summit</title>

    <script src="https://kit.fontawesome.com/bec3ffe91b.js" crossorigin="anonymous"></script> <!-- External Font Stylesheet -->

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <!-- External JQuery Script -->

    <link rel="stylesheet" type="text/css" href="../CSS/setting.css"> <!-- Internal Stylesheet -->

    <link rel="stylesheet" type="text/css" href="../CSS/styling.css"> <!-- Internal Stylesheet -->

    <link rel="shortcut icon" href="#"> <!-- Favicon -->

    </head>

    <body>

        



慕的地8271018
浏览 85回答 1
1回答

万千封印

你可以这样做。在按键事件上检查是否keycode为 80 并altkey按下。然后触发菜单的单击事件if&nbsp;(e.altKey&nbsp;&&&nbsp;keyCode&nbsp;==&nbsp;80)代码// Toggle Page List$(document).ready(function() {&nbsp; &nbsp; $('.togglePL').click(function(e) {&nbsp; &nbsp; &nbsp; &nbsp; if ($('#pageList').width() == 40) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('.textPL').show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('#pageList').width(168);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('.togglePL').css({left:'192px', transform: 'none'});&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$('#pageList a').css({fontSize:'10pt'});&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('.textPL').hide();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('#pageList').width(40);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('.togglePL').css({left:'64px', transform: 'rotate(180deg)', transitionDuration:'0.3s'});&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('#pageList a').css({fontSize:'14pt'});&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });});$(document).on('keydown', function (e) {&nbsp; &nbsp; &nbsp; &nbsp; let keyCode = e.keyCode | e.which;&nbsp; &nbsp; &nbsp; &nbsp; if (e.altKey && keyCode == 80) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$('.togglePL').trigger('click')&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; })/*&nbsp; &nbsp; ~ Copyright (c) Summit Learning Management System (made by students, for students). 2020.*/html > body {&nbsp; &nbsp; height: 100%;&nbsp; &nbsp; overflow: hidden;&nbsp; &nbsp; margin: 0;&nbsp; &nbsp; padding: 0;&nbsp; &nbsp; font-family: "Trebuchet MS", sans-serif;}#wrapper {&nbsp; &nbsp; display: flex;&nbsp; &nbsp; flex-wrap: nowrap;&nbsp; &nbsp; height: 100%;&nbsp; &nbsp; min-height: 100vh;&nbsp; &nbsp; overflow: hidden;&nbsp; &nbsp; background: #555;}/* Navigation */#navigation {&nbsp; &nbsp; display: flex;&nbsp; &nbsp; flex-direction: column;&nbsp; &nbsp; min-height: 100%;&nbsp; &nbsp; width: 40px;&nbsp; &nbsp; padding: 8px 0;&nbsp; &nbsp; background: #1b315e;}#navigation > .spacer {&nbsp; &nbsp; display: block;&nbsp; &nbsp; margin: 120px 0 0 0;}#navigation > a {&nbsp; &nbsp; display: block;&nbsp; &nbsp; margin: 0;&nbsp; &nbsp; padding: 28px 0;&nbsp; &nbsp; text-align: center;&nbsp; &nbsp; font-size: 18pt;&nbsp; &nbsp; color: #fff;}#navigation > a.active {&nbsp; &nbsp; color: #fff;&nbsp; &nbsp; border-left: 2px solid #fff;&nbsp; &nbsp; background: #2b417e;}#navigation > a:not(.active):hover {&nbsp; &nbsp; color: #fff;&nbsp; &nbsp; border-left: 2px solid #fff;&nbsp; &nbsp; background: #2b417e;&nbsp; &nbsp; opacity: 0.75;&nbsp; &nbsp; transition-duration: 0.3s;}#navigation > .navLinks {&nbsp; &nbsp; cursor: inherit;&nbsp; &nbsp; display: block;&nbsp; &nbsp; margin: 0;&nbsp; &nbsp; height: 48px;&nbsp; &nbsp; background: none;&nbsp; &nbsp; border: none;&nbsp; &nbsp; outline: none;&nbsp; &nbsp; text-align: center;&nbsp; &nbsp; font-size: 14pt;&nbsp; &nbsp; color: #fff;}#navigation > .navLinks:hover {&nbsp; &nbsp; color: #fff;&nbsp; &nbsp; background: #2b417e;&nbsp; &nbsp; transition-duration: 0.3s;}/* Page List */#pageList {&nbsp; &nbsp; display: flex;&nbsp; &nbsp; flex-direction: column;&nbsp; &nbsp; min-height: 100%;&nbsp; &nbsp; width: 168px;&nbsp; &nbsp; padding: 8px 0;&nbsp; &nbsp; justify-content: center;&nbsp; &nbsp; background: #2b417e;}#pageList > a {&nbsp; &nbsp; display: block;&nbsp; &nbsp; margin: 0;&nbsp; &nbsp; padding: 28px 0;&nbsp; &nbsp; text-transform: uppercase;&nbsp; &nbsp; text-decoration: none;&nbsp; &nbsp; text-align: center;&nbsp; &nbsp; font-size: 10pt;&nbsp; &nbsp; color: #fff;}#pageList > a.active {&nbsp; &nbsp; color: #fff;&nbsp; &nbsp; border-left: 2px solid #fff;&nbsp; &nbsp; background: #3b518e;}#pageList > a:not(.active):hover {&nbsp; &nbsp; color: #fff;&nbsp; &nbsp; border-left: 2px solid #fff;&nbsp; &nbsp; background: #3b518e;&nbsp; &nbsp; opacity: 0.75;&nbsp; &nbsp; transition-duration: 0.3s;}#pageList > .togglePL {&nbsp; &nbsp; display: block;&nbsp; &nbsp; position: absolute;&nbsp; &nbsp; top: 12px;&nbsp; &nbsp; left: 192px;&nbsp; &nbsp; height: 32px;&nbsp; &nbsp; width: 32px;&nbsp; &nbsp; background: #eee;&nbsp; &nbsp; border: 2px solid #999;&nbsp; &nbsp; border-radius: 100%;&nbsp; &nbsp; text-align: center;&nbsp; &nbsp; font-size: 10pt;&nbsp; &nbsp; color: #999;}<!doctype html><!--&nbsp; &nbsp; ~ Copyright (c) Summit Learning Management System (made by students, for students). 2020.--><html><head>&nbsp; &nbsp; <meta charset="UTF-8">&nbsp; &nbsp; <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0">&nbsp; &nbsp; <title>Welcome - Summit</title>&nbsp; &nbsp; <script src="https://kit.fontawesome.com/bec3ffe91b.js" crossorigin="anonymous"></script> <!-- External Font Stylesheet -->&nbsp; &nbsp; <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <!-- External JQuery Script -->&nbsp; &nbsp; <link rel="stylesheet" type="text/css" href="../CSS/setting.css"> <!-- Internal Stylesheet -->&nbsp; &nbsp; <link rel="stylesheet" type="text/css" href="../CSS/styling.css"> <!-- Internal Stylesheet -->&nbsp; &nbsp; <link rel="shortcut icon" href="#"> <!-- Favicon -->&nbsp; &nbsp; </head>&nbsp; &nbsp; <body>&nbsp; &nbsp; <div id="wrapper">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <!-- Navigation -->&nbsp; &nbsp; &nbsp; &nbsp; <div id="navigation">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="spacer"></div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="#" title="Home workspace" class="active"><i class="fal fa-home"></i></a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="#" title="Learning workspace"><i class="fal fa-book"></i></a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="#" title="Student management workspace"><i class="fal fa-user"></i></a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="#" title="Portal workspace"><i class="fal fa-globe"></i></a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="#" title="Administration workspace"><i class="fal fa-cog"></i></a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="spacer"></div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button title="Help" class="navLinks"><i class="fal fa-question-circle"></i></button>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button title="Quick links" class="navLinks"><i class="fal fa-bookmark"></i></button>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button title="Log out" class="navLinks"><i class="fal fa-sign-out-alt"></i></button>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <!-- Page List -->&nbsp; &nbsp; &nbsp; &nbsp; <div id="pageList">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="#" title="Dashboard"><i class="fal fa-chart-line"></i> <span class="textPL">Dashboard</span></a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="#" title="Summit messages"><i class="fal fa-comments"></i> <span class="textPL">Summit Messages</span></a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="#" title="Help"><i class="fal fa-question-circle"></i> <span class="textPL">Help</span></a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="#" title="Notices"><i class="fal fa-newspaper"></i> <span class="textPL">Notices</span></a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="#" title="Timetable"><i class="fal fa-calendar-alt"></i> <span class="textPL">Timetable</span></a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="#" title="Welcome" class="active"><i class="fal fa-hands-helping"></i> <span class="textPL">Welcome</span></a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button title="Expand/collapse the page selectors [ alt + p ]" class="togglePL"><i class="fas fa-chevron-left"></i></button>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <script src="../JS/setting.js" type="text/javascript"></script> <!-- Internal Script -->&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; </body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5