Spec-Zone.ru › Web APIs

TextUpdateEvent: свойство selectionStart

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

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

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

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

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

Значение

Значение типа Number.

Примеры

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

В этом примере показано, как использовать свойство selectionStart для отображения выделенного текста в обработчике событий 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-selectionstart

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

Десктопные Мобильные
Chrome Edge Firefox Opera Safari Chrome Android Firefox для Android Opera Android Safari на iOS Samsung Internet WebView Android
selectionStart 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/selectionStart

Spec-Zone.ru

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