I've been pulling my hair out trying to figure out the best way to fix this problem. Perhaps someone will have fun with this.
I have a div with contentEditable="true" that works fine for nearly everything. One exception is inside Firefox when the user uses CTRL-A to select everything and then copies/pastes. The select operation includes the tags for div i开发者_运维问答tself! As far as I was able to search, this bug was supposedly fixed by Mozilla, but that just isn't true. The div just continually is pasted inside itself.
I've looked into editing the clipboard once the user has copied it, but Firefox doesn't seem too friendly towards that unless you use flash, which isn't very desirable. I've also thought of catching the offending tags on the other end, when the user does a paste, but the paste events put the actual text in the div after any javascript event handler has finished. Is there a way to edit the text being pasted before it happens?
Actually it is a FireFox browser issue.You can use the following function to fix the issue.
function disableCtrlKeyCombination(event){
var keyCode = event.keyCode;
if (event.ctrlKey && keyCode==86) { //CTRL+V
event.preventDefault();
document.getElementById("divId").textContent = system.getClipboard().getData("text");
return false;
} else if (event.ctrlKey && keyCode==67) { //CTRL+C (Copy)
event.preventDefault();
system.getClipboard().setData("text",document.getElementById("divId").textContent);
return false;
} else {
return true;
}
}
Note : This will work only in Firefox. In other browsers it works fine by default.
精彩评论