听着关键pressed事件剃刀 [英] Listening to the keyPressed event, Razor

查看:176
本文介绍了听着关键pressed事件剃刀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

现在我要听左/右箭头keydown事件和火一样的动作为这些链接做的。据我了解,这不能没有JS帮助下完成的,对不对?我是pretty新的ASP.NET特别是对JS,这样可以ü的人只是告诉我要达到上述的最佳方式?

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?

推荐答案

您将需要添加的js的片段视图

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>

这篇关于听着关键pressed事件剃刀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆