簡體   English   中英

ember.js ReferenceError:firefox中未定義事件

[英]ember.js ReferenceError: event is not defined in firefox

我想在ember動作中訪問事件對象以檢查目標元素。 這在Chrome和Safari中有效,但在Firefox 25.0中無效。

我什至沒有收到錯誤消息。 我如何在ember動作中訪問事件對象,或者有沒有余燼的方法?

繁殖方法:

  1. 打開我的小提琴
  2. 單擊div或“單擊我”鏈接,應打開一個警報框
  3. 在chrome和firefox中測試這些

HTML

  <script type="text/x-handlebars" data-template-name="application">
      <h1>Click the link in the following div</h1>
      {{outlet}}
  </script>
  <script type="text/x-handlebars" data-template-name="index">
      {{test-a}}
  </script>
  <script type="text/x-handlebars" data-template-name="components/test-a">
    <div class="index" {{action 'edit' on="click"}}>
        <a href="http://www.example.com" target="_blank">Click me</a>
    </div>
  </script>

咖啡

App = Ember.Application.create({});

App.TestAComponent = Ember.Component.extend
    actions:
        edit: ->
            if event.toElement.nodeName.toUpperCase() == 'A'
                return true # should bubble to the window but is a component
            # do something for editing here
            alert('div clicked')
            false

CSS

h1 { font-size: 1.6em; padding-bottom: 10px; }
h2 { font-size: 1.4em; }
ul { padding: 15px; font-size: 1.4em; color: green; }
.index { background-color: #666; padding: 20px; }

http://jsfiddle.net/mszrnyi/3REEj/2/

一些瀏覽器在window.event創建一個全局事件對象,但這不是跨瀏覽器。

使用動作處理程序,您將無法正常工作。 因為操作是處理瀏覽器事件的頂級方法。 因此,不會傳遞任何事件對象,並且在觸發{{action}}會執行event.preventDefault() ,因此您的鏈接不會打開新窗口。

您只需click即可使用。 這是處理瀏覽器事件的底層方法,因此您將在第一個參數中獲得事件對象。 然后根據布爾值返回的氣泡預期行為:

 App.TestAComponent = Ember.Component.extend    
    click: (event) ->                
        if event.toElement.nodeName.toUpperCase() == 'A'
            alert('link clicked')
            return true # should bubble to the window but is a component
        # do something for editing here
        alert('div clicked')       
        false

另外,還需要從components/test-a模板中刪除{{action 'edit' on="click"}} 否則將執行event.preventDefault()

  <script type="text/x-handlebars" data-template-name="components/test-a">
    <div class="index">
        <a href="http://www.example.com" target="_blank">Click me</a>
    </div>
  </script>

這是更新的小提琴http://jsfiddle.net/8Ephq/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM