DOM 요소가있는 텍스트를 클래스로 바꾸는 간단한 구문 형광펜 작업을하고 있습니다. |js contenteditable - 새로 삽입 된 요소에 쓰지 못하도록합니다.
말은, 나는
<div contenteditable="true">
Some text. | And some other text.
</div>
이 커서는에있다 파이프
// 사용자가 입력하는 경우 사용자가 입력하고 나는 그것이 다음 삽입 요소
<div contenteditable="true">
Some text. <span class="highlight-foo">foo</span>| And some other text.
</div>
후 선택을 설정하지만, 대신, 당신은에 foo
<div contenteditable="true">
Some text. foo| And some other text.
</div>
을 입력 // 경우 아무 상관 없어.
// 타입 바
<div contenteditable="true">
Some text. <span class="highlight-foo">foobar|</span> And some other text.
</div>
내가 그 싶지 않아,하지만 난 바로 새로 삽입 된 요소 뒤에 선택을 설정할 수 없습니다.
<div contenteditable="true">
Some text. <span class="highlight-foo">foo</span>bar| And some other text.
</div>
여기 강조 및 교체를 수행하는 JS ..
...
// chunk is the variable that holds foo, if we stick the the above example..
// select it
range.setStart(range.startContainer, range.startOffset-chunk.length);
// add it to the range
sel.addRange(range);
// replace it..then set the range to the textNode after the span
// because after the injection selection.anchorNode is the textNode inside the span..
// in chrome, however, this below sets the range correctly.
range.setStart(sel.anchorNode.parentNode.nextSibling, 0);
// and it's all good until now, but adding the range to the selection does nothing..
sel.removeAllRanges();
sel.addRange(range)
// the selection is still inside the span..
어떻게 그것을 해결할 수있어? o 나는 그것에 관해 많은 것을 읽었는데 여기에도 상당한 양의 질문을 보았지만이 특별한 문제에 대해서는 아무것도 보지 못했습니다.
게코 (gecko)에서 작동하는 것으로 보이기 때문에 그렇습니다. 감사합니다. 나는 대답을 수락합니다. 그러한 대안을 사용하기를 꺼리는데, 지금은 다른 방법이 없다고 생각됩니다. 감사합니다 – tenshou
@ tenshou : 나는 다른 더 나은 방법을 발견했다. 나는 나의 대답을 업데이트했다. –
그게 좋네. 고마워. 공백이 포함 된 추가 작업을 제거합니다. 그것은 절대적으로 효과가있는 것처럼 보입니다. 클릭하거나 화살표 키를 사용하여 링크를 입력 할 수는 없지만 게코의 경계가 좁은 반면, 오른쪽을 향하면 커서를 링크에 바로 놓을 수 있습니다. , 문자를 보는 것은 여전히 같은 오프셋에있을 것입니다. 그러나 그것은 또 다른 이야기입니다. 고맙습니다! – tenshou