Listening to the keyPressed event, Razor

2019-09-09 21:25发布

问题:

<body>
<table align="center" width="100%" height="100%">
    <tr>
        <td align="center" id="previousPhoto">
            @if (Model.HasPreviousPhoto)
            {
                if (Model.CurrentPhotoIndex == 0)
                {
                        <a href="@HrefHelper.ToPhoto(Model.Title, Model.CurrentPageIndex - 1, maxPhotosOnThePage)">
                            <section class="navSection">
                                <img class="previousPhoto" src="@Url.Content("~/Content/Icons/arrows.png")" />
                            </section>
                        </a>

                    }
                    else
                    {
                        <a href="@HrefHelper.ToPhoto(Model.Title, Model.CurrentPageIndex, Model.CurrentPhotoIndex - 1)">
                            <section class="navSection">
                                <img class="previousPhoto" src="@Url.Content("~/Content/Icons/arrows.png")" />
                            </section>
                        </a>
                    }
            }
            else
            {
                <section class="navSection"></section>
            }
        </td>
        <td class="photoPlaceholder" align="center">
            <section class="photoSection">
                <img src="@Model.CurrentPhoto.GenerateSrcHTML()" />
            </section>
        </td>
        <td align="center" id="nextPhoto">
            @if (Model.HasNextPhoto)
            {
                if (Model.CurrentPhotoIndex == maxPhotosOnThePage)
                {
                        <a href="@HrefHelper.ToPhoto(Model.Title, Model.CurrentPageIndex + 1, 0)">
                            <section class="navSection">
                                <img src="@Url.Content("~/Content/Icons/arrows.png")" />
                            </section>
                        </a>
                }
                else
                {
                        <a href="@HrefHelper.ToPhoto(Model.Title, Model.CurrentPageIndex, Model.CurrentPhotoIndex + 1)">
                            <section class="navSection">
                                <img src="@Url.Content("~/Content/Icons/arrows.png")" />
                            </section>
                        </a>
                }
            }
            else
            {
                <section class="navSection"></section>
            }
        </td>
    </tr>
</table>

Now I need to listen to the left/right-arrows keydown event and to fire same action as those links do. As I understand, this can't be done without JS help, right? I am pretty new to ASP.NET and especially to JS, so can someone of u just show me the best way to achieve described above?

回答1:

You will need to add a snippet of js in the view

<script>
    document.addEventListener('keydown', function(e){
        //37 is left arrow, 39 is right arrow
        if(e.which === 37){
            document.getElementById('previousPhoto').getElementsByTagName('a')[0].click();
        } else if (e.which === 39) {
            document.getElementById('nextPhoto').getElementsByTagName('a')[0].click();

    }
});

</script>