104 html javascript cross-browser caret contenteditable
Chrome中的输出:
<div id="content" contenteditable="true" style="border:1px solid #000;width:500px;height:40px;">
hey
<div>what's up?</div>
<div>
<button id="insert_caret"></button>
Run Code Online (Sandbox Code Playgroud)
我相信FF看起来像这样:
hey
<br />
what's up?
Run Code Online (Sandbox Code Playgroud)
在IE中:
hey
<p>what's up?</p>
Run Code Online (Sandbox Code Playgroud)
不幸的是,没有很好的方法来制作它,以便每个浏览器插入一个<br />
而不是div或p-tag,或者至少我在网上找不到任何东西.
无论如何,我现在要做的是,当我按下按钮时,我希望将插入符号设置在文本的末尾,所以它应该看起来像这样:
hey
what's up?|
Run Code Online (Sandbox Code Playgroud)
任何方式这样做,所以它适用于所有浏览器?
例:
$(document).ready(function()
{
$('#insert_caret').click(function()
{
var ele = $('#content');
var length = ele.html().length;
ele.focus();
//set caret -> end pos
}
}
Run Code Online (Sandbox Code Playgroud)
Tim*_*own 267
以下函数将在所有主流浏览器中执行:
function placeCaretAtEnd(el) {
el.focus();
if (typeof window.getSelection != "undefined"
&& typeof document.createRange != "undefined") {
var range = document.createRange();
range.selectNodeContents(el);
range.collapse(false);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
} else if (typeof document.body.createTextRange != "undefined") {
var textRange = document.body.createTextRange();
textRange.moveToElementText(el);
textRange.collapse(false);
textRange.select();
}
}
placeCaretAtEnd( document.querySelector('p') );
Run Code Online (Sandbox Code Playgroud)
p{ padding:.5em; border:1px solid black; }
Run Code Online (Sandbox Code Playgroud)
<p contentEditable>foo bar </p>
Run Code Online (Sandbox Code Playgroud)
将插入符号放在开头几乎是相同的:它只需要更改传入调用的布尔值collapse()
.这是一个创建将插入符号放在开头和结尾的函数的示例:
function createCaretPlacer(atStart) {
return function(el) {
el.focus();
if (typeof window.getSelection != "undefined"
&& typeof document.createRange != "undefined") {
var range = document.createRange();
range.selectNodeContents(el);
range.collapse(atStart);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
} else if (typeof document.body.createTextRange != "undefined") {
var textRange = document.body.createTextRange();
textRange.moveToElementText(el);
textRange.collapse(atStart);
textRange.select();
}
};
}
var placeCaretAtStart = createCaretPlacer(true);
var placeCaretAtEnd = createCaretPlacer(false);
Run Code Online (Sandbox Code Playgroud)
不幸的是,Tim出色的回答仅对我有用,但只能放在结尾,而我必须稍加修改。
function setCaret(target, isStart) {
const range = document.createRange();
const sel = window.getSelection();
if (isStart){
const newText = document.createTextNode('');
target.appendChild(newText);
range.setStart(target.childNodes[0], 0);
}
else {
range.selectNodeContents(target);
}
range.collapse(isStart);
sel.removeAllRanges();
sel.addRange(range);
target.focus();
target.select();
}
Run Code Online (Sandbox Code Playgroud)
不知道但如果focus()
和select()
实际需要。
这个(实时)示例显示了一个简短的简单函数,setCaretAtStartEnd
它带有两个参数;用于放置插入符号的(可编辑)节点和一个指示放置位置的布尔值(节点的开始或结束)
const editableElm = document.querySelector('[contenteditable]');
document.querySelectorAll('button').forEach((elm, idx) =>
elm.addEventListener('click', () => {
editableElm.focus()
setCaretAtStartEnd(editableElm, idx)
})
)
function setCaretAtStartEnd( node, atEnd ){
const sel = document.getSelection();
node = node.firstChild;
if( sel.rangeCount ){
['Start', 'End'].forEach(pos =>
sel.getRangeAt(0)["set" + pos](node, atEnd ? node.length : 0)
)
}
}
Run Code Online (Sandbox Code Playgroud)
[contenteditable]{ padding:5px; border:1px solid; }
Run Code Online (Sandbox Code Playgroud)
<h1 contenteditable>Place the caret anywhere</h1>
<br>
<button>Move caret to start</button>
<button>Move caret to end</button>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
51284 次 |
最近记录: |