为了账号安全,请及时绑定邮箱和手机立即绑定

将键盘元素添加到按钮

将键盘元素添加到按钮

慕的地8271018 2023-10-10 16:16:58
我在这里有一个名为的按钮.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>        
查看完整描述

1 回答

?
万千封印

TA贡献1891条经验 获得超3个赞

你可以这样做。在按键事件上检查是否keycode为 80 并altkey按下。然后触发菜单的单击事件

if (e.altKey && keyCode == 80)

代码

// 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'});

        }

    });

});


$(document).on('keydown', function (e) {

        let keyCode = e.keyCode | e.which;

        if (e.altKey && keyCode == 80) {

           $('.togglePL').trigger('click')

        }

      })

/*

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

*/


html > body {

    height: 100%;

    overflow: hidden;

    margin: 0;

    padding: 0;

    font-family: "Trebuchet MS", sans-serif;

}


#wrapper {

    display: flex;

    flex-wrap: nowrap;

    height: 100%;

    min-height: 100vh;

    overflow: hidden;

    background: #555;

}


/* Navigation */

#navigation {

    display: flex;

    flex-direction: column;

    min-height: 100%;

    width: 40px;

    padding: 8px 0;

    background: #1b315e;

}


#navigation > .spacer {

    display: block;

    margin: 120px 0 0 0;

}


#navigation > a {

    display: block;

    margin: 0;

    padding: 28px 0;

    text-align: center;

    font-size: 18pt;

    color: #fff;

}


#navigation > a.active {

    color: #fff;

    border-left: 2px solid #fff;

    background: #2b417e;

}


#navigation > a:not(.active):hover {

    color: #fff;

    border-left: 2px solid #fff;

    background: #2b417e;

    opacity: 0.75;

    transition-duration: 0.3s;

}


#navigation > .navLinks {

    cursor: inherit;

    display: block;

    margin: 0;

    height: 48px;

    background: none;

    border: none;

    outline: none;

    text-align: center;

    font-size: 14pt;

    color: #fff;

}


#navigation > .navLinks:hover {

    color: #fff;

    background: #2b417e;

    transition-duration: 0.3s;

}


/* Page List */

#pageList {

    display: flex;

    flex-direction: column;

    min-height: 100%;

    width: 168px;

    padding: 8px 0;

    justify-content: center;

    background: #2b417e;

}


#pageList > a {

    display: block;

    margin: 0;

    padding: 28px 0;

    text-transform: uppercase;

    text-decoration: none;

    text-align: center;

    font-size: 10pt;

    color: #fff;

}


#pageList > a.active {

    color: #fff;

    border-left: 2px solid #fff;

    background: #3b518e;

}


#pageList > a:not(.active):hover {

    color: #fff;

    border-left: 2px solid #fff;

    background: #3b518e;

    opacity: 0.75;

    transition-duration: 0.3s;

}


#pageList > .togglePL {

    display: block;

    position: absolute;

    top: 12px;

    left: 192px;

    height: 32px;

    width: 32px;

    background: #eee;

    border: 2px solid #999;

    border-radius: 100%;

    text-align: center;

    font-size: 10pt;

    color: #999;

}

<!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>

    <div id="wrapper">

        

        <!-- Navigation -->

        <div id="navigation">

            <div class="spacer"></div>

            <a href="#" title="Home workspace" class="active"><i class="fal fa-home"></i></a>

            <a href="#" title="Learning workspace"><i class="fal fa-book"></i></a>

            <a href="#" title="Student management workspace"><i class="fal fa-user"></i></a>

            <a href="#" title="Portal workspace"><i class="fal fa-globe"></i></a>

            <a href="#" title="Administration workspace"><i class="fal fa-cog"></i></a>

            <div class="spacer"></div>

            <button title="Help" class="navLinks"><i class="fal fa-question-circle"></i></button>

            <button title="Quick links" class="navLinks"><i class="fal fa-bookmark"></i></button>

            <button title="Log out" class="navLinks"><i class="fal fa-sign-out-alt"></i></button>

        </div>

        

        <!-- Page List -->

        <div id="pageList">

            <a href="#" title="Dashboard"><i class="fal fa-chart-line"></i> <span class="textPL">Dashboard</span></a>

            <a href="#" title="Summit messages"><i class="fal fa-comments"></i> <span class="textPL">Summit Messages</span></a>

            <a href="#" title="Help"><i class="fal fa-question-circle"></i> <span class="textPL">Help</span></a>

            <a href="#" title="Notices"><i class="fal fa-newspaper"></i> <span class="textPL">Notices</span></a>

            <a href="#" title="Timetable"><i class="fal fa-calendar-alt"></i> <span class="textPL">Timetable</span></a>

            <a href="#" title="Welcome" class="active"><i class="fal fa-hands-helping"></i> <span class="textPL">Welcome</span></a>

            <button title="Expand/collapse the page selectors [ alt + p ]" class="togglePL"><i class="fas fa-chevron-left"></i></button>

        </div>

        

        </div>

        

        <script src="../JS/setting.js" type="text/javascript"></script> <!-- Internal Script -->

        

    </body>

</html>


查看完整回答
反对 回复 2023-10-10
  • 1 回答
  • 0 关注
  • 84 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信