我试图复制一些我在几个博客上看到的东西,并且在Facebook上有所使用,并且这是通过按一个键移动到下一篇文章。在Facebook上按下“j”键。我不擅长Javascript,并且在这里发现了一些关于如何去做的文章,但是我觉得我错过了一些东西。这是我的视图代码,请告诉我我做错了什么。警报甚至不会触发。谢谢。似乎无法获得“keyup”的工作
@{
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>
您忘记了document.ready! – adeneo
@adeneo这有什么关系?这个事件被绑定到'document' – Ian
@Ian - 哦,没有注意到,只是注意到事件处理程序后的标记。应该学习阅读。 – adeneo