我做了一些測試,我想我是能夠重現行爲。我只使用Google Chrome和Firefox測試了此解決方案,因此它在許多瀏覽器中可能會遇到問題。我懷疑Google Plus在無法使用此技術時會優雅地降低用戶體驗。
要實現此功能,我貼事件中重定向將焦點轉移到屏幕之外的股利。該div收到粘貼內容,並在超時後,我得到的文本div(使用jquery刪除html標籤)。示例代碼:
var object = this;
this.$editable.on("paste", function(evt) {
object.saveCursorPosition();
var textarea= $("<div contenteditable></div>");
textarea.css("position", "absolute").css("left", "-1000px").css("top", object.$editable.offset().top + "px").attr("id","pasteHelper").appendTo("body");
textarea.html('<BR>');
textarea.focus();
setTimeout(function() {
object.$editable.focus();
object.restoreCursorPosition();
object.insertTextAtCursor(textarea.text());
textarea.remove();
}, 0);
});