나는이 표시 버튼 UI에, 사용자 선택이 빨간색으로 표시되고, 클릭합니다. 문제 없습니다. 나는 이것을 통해 달성document.execCommand("insertHTML")
그러나 이전 선택 표시의 교차점 인 새 선택을 만들면 이전 선택의 빨간색 표시가 사라져야한다는 추가 요구 사항이 있습니다.
예로서:
다음 이미지에서 : 이것 과 테스트 가 표시되어 있습니다. 내가 선택하면 이제 그의 TES입니다 시작부터하고 표시를 클릭, 오래된 표시 이 와 테스트는 멀리 가야 만 자신은 TES는 교차로가 있기 때문에 표시해야합니다.
암호:
const button = document.getElementById("button");
button.addEventListener('click', ()=>{
const s = window.getSelection();
const selectionStr = s.toString();
document.execCommand("insertHTML", false, `<span class="bg-red">${selectionStr}<span>`);
})
.bg-red {
background: red;
}
<div contenteditable="true">
this is testing this is testing this is testing
</div>
<button id="button">mark</button>
