窗口粘贴事件侦听器未在IE 11中触发

时间:2018-05-31 20:23:57

标签: javascript typescript window clipboard addeventlistener

这是我在chrome中完美运行的代码

function load() {
  let ext = null;
  var postRef = firebase.database().ref('posts/' + viewNumber + '/fileType');
  postRef.on('value', function (snapshot) {
    var value = snapshot.val();

    if (value === null) {
      return;
    }

    ext = value;
    console.log(viewNumber);
    console.log(ext);
    var storage = firebase.storage();
    var pathReference = storage.ref('posts/' + viewNumber + '.' + ext);
    pathReference.getDownloadURL().then(function (url) {
      // Or inserted into an <img> element:
      var img = document.getElementById('post');
      img.src = url;
    });
    // A full list of error codes is available at
    // https://firebase.google.com/docs/storage/web/handle-errors
  });
};

在Chrome中,如果用户在我的组件中选择Ctrl + V(在这种情况下是自定义网格),则会触发此操作

public ngOnInit() {
    window.addEventListener('paste', this.InsertNewRowsBeforePaste.bind(this));
  }

问题出在IE11中,InserNewRowsBeforePaste永远不会被触发,因为我的控制台从不记录事件或窗口。那是为什么?

1 个答案:

答案 0 :(得分:0)

我明白了。您必须在IE中捕获将模仿Ctrl + V行​​为的按键事件,然后您可以从那里访问剪贴板

this.global = this.renderer.listen('document', 'keydown', (event) => {
        if (event.ctrlKey === true && event.key === 'v') {
          const clipboardData = window['clipboardData'].getData('Text');
          if (clipboardData) {
            // Call any method to manipulate the clipboard data here
          }
        }
      });