2013-07-11 85 views
0

我试图复制一些我在几个博客上看到的东西,并且在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> 
+0

您忘记了document.ready! – adeneo

+2

@adeneo这有什么关系?这个事件被绑定到'document' – Ian

+0

@Ian - 哦,没有注意到,只是注意到事件处理程序后的标记。应该学习阅读。 – adeneo

回答

2

有导入的jQuery的文件,原因为什么我问的是你还没有标记与jQuery

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

也许你错过了加入这个脚本。

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

非常感谢。 VS2012已将它加载到项目中,但它不在_Layout.cshtml中。有用。 – XstreamINsanity

+0

@XstreamINsanity我很高兴能够帮助你。 – Praveen

+0

我没有用jQuery标记的原因是因为当我在Google上查找“javascript keyup”时,从我的瞥见中,它看起来好像它不一定属于jQuery,而是javascript。我应该更彻底地阅读一些东西。 :) – XstreamINsanity

相关问题