0
我從這個thread學會了如何在一個CONTENTEDITABLE DIV插入元素之後設置插入符。不過,我想阻止軟鍵盤打開。防止軟鍵盤從開放使用selection.addRange時()
我已經CONTENTEDITABLE監聽onfocus事件fiddle based on the above thread到的preventDefault/stopPropagation,但沒有奏效。任何建議都會很棒!
function insertNodeAtCaret(node) {
if (typeof window.getSelection != "undefined") {
var sel = window.getSelection();
if (sel.rangeCount) {
var range = sel.getRangeAt(0);
range.collapse(false);
range.insertNode(node);
range = range.cloneRange();
range.selectNodeContents(node);
range.collapse(false);
sel.removeAllRanges();
sel.addRange(range);
}
} else if (typeof document.selection != "undefined" && document.selection.type != "Control") {
var html = (node.nodeType == 1) ? node.outerHTML : node.data;
var id = "marker_" + ("" + Math.random()).slice(2);
html += '<span id="' + id + '"></span>';
var textRange = document.selection.createRange();
textRange.collapse(false);
textRange.pasteHTML(html);
var markerSpan = document.getElementById(id);
textRange.moveToElementText(markerSpan);
textRange.select();
markerSpan.parentNode.removeChild(markerSpan);
}
}
function blockKeyboard(event) {
event.stopImmediatePropagation();
event.preventDefault();
event.stopPropagation();
console.log(event);
}