2017-03-22 1 views
0

私はこれを読んでいます:https://developer.mozilla.org/en-US/docs/Web/API/Document/createEventと古いバージョンでは、古い方法を提示するときにいくつかの細部を除いています。新しいMouseEventを作成するには、古き良き方法がありますか?

私は、特定のコードを持っている:一部の環境で適切に実行されていない

HTMLElement.prototype.mouseDownLeftButton = function() { 
    var event = new MouseEvent('mousedown', 
    { 
     'which': 1, 
     'view': window, 
     'bubbles': true, 
     'cancelable': true 
    }); 
    this.dispatchEvent(event); 
}; 

。したがって、私は、昔ながらの方法でバージョンを書いた:

HTMLElement.prototype.mouseDownLeftButton = function() { 
    var event = document.createEvent("MouseEvent"); 
    event.initEvent("mousedown", true, false); 
    event.setAttribute("which", 1);//how to do it correctly? 
    event.setAttribute("view", window);//how to do it correctly? 
    this.dispatchEvent(event); 
}; 

を残念ながら私は TypeError: event.setAttribute is not a function

質問を得る:が正しく古いやり方でwindow1からwhichviewを設定する方法は? which: 1を明確にするために、マウスの左ボタンが「押し下げられた」と言います。

答えて

1

あなたがevent.currentTarget.setAttributeを使用して

1 - Left 
2 - Middle 
3 - Right 

JS

HTMLElement.prototype.mouseDownLeftButton = function() { 
    var event = document.createEvent("MouseEvent"); 
    event.initEvent("mousedown", true, false); 
    event.setAttribute("which", event.which);//how to do it correctly? 
    event.setAttribute("view", window);//how to do it correctly? 
    this.dispatchEvent(event); 
}; 
+0

ありがとう検出しますちょうどevent.whichプロパティを、使用する必要があるが、私は得る: 'null'なのでのプロパティ 'のsetAttribute' を読み取ることができません。 – Yoda

+0

@Yodaイベントを何かに添付しましたか? –

+0

ええ、私は 'event.which'と' event.view'だけで助けました。しかし、私は他の環境をチェックする必要があります。 – Yoda

関連する問題