【问题标题】:emberjs + rails, capture keypress eventemberjs + rails,捕获按键事件
【发布时间】:2013-06-23 03:50:43
【问题描述】:

我在普通的html文件中测试js脚本

$(document).ready ->
  $("#subject").keyup (event) ->
    alert "wll"

它有效。

但是当我在 emberjs + rails 项目中使用它时。 它不起作用

这是怎么回事?

【问题讨论】:

    标签: jquery ruby-on-rails ember.js keypress enter


    【解决方案1】:

    有一条规则:所有事件都应该放在views (http://emberjs.com/guides/views/) 中。

    你有所有事件的列表:

    EVENT NAMES
    Possible events names for any of the responding approaches described above are:
    
    Touch events:
    
    touchStart
    touchMove
    touchEnd
    touchCancel
    Keyboard events
    
    keyDown
    keyUp
    keyPress
    Mouse events
    
    mouseDown
    mouseUp
    contextMenu
    click
    doubleClick
    mouseMove
    focusIn
    focusOut
    mouseEnter
    mouseLeave
    Form events:
    
    submit
    change
    focusIn
    focusOut
    input
    HTML5 drag and drop events:
    
    dragStart
    drag
    dragEnter
    dragLeave
    drop
    dragEnd
    

    这是一个简单的例子,你可以如何使用它:

    HTML

    <script type="text/x-handlebars" data-template-name="application">
      {{outlet}}
    </script>
    <script type="text/x-handlebars" data-template-name="index">
      <div class='box'>Test</div>
    </script>
    

    JS

    App = Ember.Application.create({});
    
    App.IndexView = Ember.View.extend({
        // This is only for set the focus on our view.
        didInsertElement: function() {
          return this.$().attr({ tabindex: 1 }), this.$().focus();
        },
        keyPress: function(event) {
            console.log("KeyCode: " + event.keyCode);
        },
    });
    

    【讨论】:

      【解决方案2】:

      使用视图您可以简单地做到这一点

      AView = Ember.View.extend({
         keyUp: function(event){
             alert('wll');
         }
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-19
        • 1970-01-01
        • 2011-05-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-04
        相关资源
        最近更新 更多