IE11 में, execCommand अच्छी तरह से काम नहीं करता है। मैं IE11 के लिए नीचे दिए गए कोड का उपयोग करता हूं
<div class="wmd-input" id="wmd-input-md" contenteditable=true>
, मेरा डिव बॉक्स है।
मैं window.clipboardData से क्लिपबोर्ड डेटा पढ़ता हूं और div के पाठ को संशोधित करता हूं और कैरेट देता हूं।
मैं कैरेट सेट करने के लिए टाइमआउट देता हूं, क्योंकि अगर मैं टाइमआउट सेट नहीं करता हूं, तो एक कैरेट div के अंत में जाता है।
और आपको IE11 में क्लिपबोर्डडाटा को नीचे के तरीके से पढ़ना चाहिए। यदि आप ऐसा नहीं करते हैं, तो न्यूलाइन बैक्टीरिया को ठीक से संभाला नहीं जाता है, इसलिए कैरेट गलत हो जाता है।
var tempDiv = document.createElement("div");
tempDiv.textContent = window.clipboardData.getData("text");
var text = tempDiv.textContent;
IE11 और क्रोम पर परीक्षण किया गया। यह IE9 पर काम नहीं कर सकता है
document.getElementById("wmd-input-md").addEventListener("paste", function (e) {
if (!e.clipboardData) {
//For IE11
e.preventDefault();
e.stopPropagation();
var tempDiv = document.createElement("div");
tempDiv.textContent = window.clipboardData.getData("text");
var text = tempDiv.textContent;
var selection = document.getSelection();
var start = selection.anchorOffset > selection.focusOffset ? selection.focusOffset : selection.anchorOffset;
var end = selection.anchorOffset > selection.focusOffset ? selection.anchorOffset : selection.focusOffset;
selection.removeAllRanges();
setTimeout(function () {
$(".wmd-input").text($(".wmd-input").text().substring(0, start)
+ text
+ $(".wmd-input").text().substring(end));
var range = document.createRange();
range.setStart(document.getElementsByClassName("wmd-input")[0].firstChild, start + text.length);
range.setEnd(document.getElementsByClassName("wmd-input")[0].firstChild, start + text.length);
selection.addRange(range);
}, 1);
} else {
//For Chrome
e.preventDefault();
var text = e.clipboardData.getData("text");
var selection = document.getSelection();
var start = selection.anchorOffset > selection.focusOffset ? selection.focusOffset : selection.anchorOffset;
var end = selection.anchorOffset > selection.focusOffset ? selection.anchorOffset : selection.focusOffset;
$(this).text($(this).text().substring(0, start)
+ text
+ $(this).text().substring(end));
var range = document.createRange();
range.setStart($(this)[0].firstChild, start + text.length);
range.setEnd($(this)[0].firstChild, start + text.length);
selection.removeAllRanges();
selection.addRange(range);
}
}, false);