简体   繁体   English

似乎无法让“keyup”工作

[英]Can't seem to get “keyup” to work

I'm trying to replicate something I've seen on a few blogs and is somewhat used on Facebook, and that's moving to the next post by pressing a key. 我试图复制一些我在几个博客上看过的东西,并且在Facebook上有所使用,并且通过按键移动到下一个帖子。 On Facebook it's pressing the "j" key. 在Facebook上它按下了“j”键。 I'm not very good at Javascript, and have found a few articles on here in regards to how to do it, but I feel like I'm missing something. 我不是很擅长Javascript,并且在这里找到了一些关于如何做的文章,但我觉得我错过了一些东西。 Here's my View code, please show me what I'm doing wrong. 这是我的查看代码,请告诉我我做错了什么。 The alerts aren't even firing. 警报甚至没有开火。 Thanks. 谢谢。

@{
    ViewBag.Title = "Index";
}

<script>
    $(document).keyup(function (e) {
        alert("entered function");
        if (e.keyCode == 90 || e.keyCode == 16) {
            alert("entered conditional");
            var ta = document.getElementsByClassName('post_unread')[0];
            ta.removeAttribute('class', 'post_unread');
            ta.addAttribute('class', 'post_reading');
        }
    });
</script>

<div>
    <div class="forum_post_wrapper">
        <div class="forum_post">

        </div>
        <div class="forum_post_wrapper">
            <div class="forum_post post_unread">

            </div>
            <div class="forum_post_wrapper">
                <div class="forum_post post_unread">

                </div>
            </div>
        </div>
    </div>
</div>

Have you imported the jQuery file, the reason why I ask is you've not tagged with jQuery 你导入了jQuery文件, 我问的原因是你没有用jQuery标记

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js" />

Maybe you missed to add this script. 也许你错过了添加这个脚本。

$(document).keyup(function (e) { // this belongs to jQuery

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM