Spec-Zone.ru › Web APIs

TextUpdateEvent: свойство selectionEnd

Ограниченная доступность

Эта функция не относится к Baseline, поскольку она не работает в некоторых из наиболее широко используемых браузеров.

  • Узнать больше
  • Полная совместимость
  • Отправить отзыв

Экспериментально: Это экспериментальная технология
Перед использованием в рабочей среде внимательно проверьте таблицу совместимости браузеров.

Это TextUpdateEvent.selectionEnd только для чтения свойство указывает положение конца выделения (или курсора) в текстовом содержимом редактируемой области, прикреплённой к объекту EditContext.

Значение

A Number.

Примеры

Использование textupdate для отображения отредактированного текста и выделения пользователя

В этом примере показано, как использовать свойство selectionEnd для отображения выделенного текста внутри обработчика событий textupdate.

#editor {
  height: 200px;
  background: #eee;
  color: black;
}

.selection {
  display: inline-block;
  vertical-align: bottom;
  background: blue;
  color: white;
  min-width: 2px;
  height: 3ex;
}
<div id="editor"></div>
const editorEl = document.getElementById("editor");
const editContext = new EditContext();
editorEl.editContext = editContext;

editContext.addEventListener("textupdate", (e) => {
  // Clear the current content.
  editorEl.textContent = "";

  const text = editContext.text;
  const { selectionStart, selectionEnd } = e;

  // Render the text before the selection.
  const textBefore = document.createElement("span");
  textBefore.textContent = text.substring(0, selectionStart);

  // Render the selected text, or caret.
  const textSelected = document.createElement("span");
  textSelected.classList.add("selection");
  textSelected.textContent = text.substring(selectionStart, selectionEnd);

  // Render the text after the selection.
  const textAfter = document.createElement("span");
  textAfter.textContent = text.substring(selectionEnd);

  editorEl.appendChild(textBefore);
  editorEl.appendChild(textSelected);
  editorEl.appendChild(textAfter);

  console.log(`Text before selection: ${textBefore.textContent}`);
  console.log(`Selected text: ${textSelected.textContent}`);
  console.log(`Text after selection: ${textAfter.textContent}`);
});

Спецификации

Спецификация
API EditContext
# dom-textupdateevent-selectionend

Совместимость браузеров

Рабочий стол Мобильные устройства
Chrome Edge Firefox Opera Safari Chrome Android Firefox для Android Opera Android Safari на iOS Samsung Internet WebView Android
selectionEnd 121 121 Нет 107 Нет 121 Нет 81 Нет 25.0 121

© 2005–2024 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/TextUpdateEvent/selectionEnd

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API