Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Take a look at the <a href="http://www.w3.org/TR/DOM-Level-2-Traversal-Range/ranges.html" rel="noreferrer">DOM Range spec</a>. You can get a <code>Range</code> from the user selection in Firefox using:</p> <pre><code>var range = window.getSelection().getRangeAt(0); </code></pre> <p>Note that some browsers, including Firefox, allow multiple selections, which can be accessed via the <code>getRangeAt()</code> method of the selection.</p> <p>The <code>Range</code> is expressed in terms of DOM nodes and offsets within those nodes. Once you've got your <code>Range</code>, it's not straightforward to do exactly what you want, since the range's boundaries could be in different nodes at different levels of the DOM tree, so simply surrounding the Range's content with a tag is not always possible. You may be able to do something like the following, although it will create a new block element to contain the selected content:</p> <pre><code>var range = window.getSelection().getRangeAt(0); var selectionContents = range.extractContents(); var div = document.createElement("div"); div.style.color = "yellow"; div.appendChild(selectionContents); range.insertNode(div); </code></pre> <p>Another, hacky, alternative is to use the <code>execCommand()</code> method of <code>document</code> to modify the selection (e.g. by setting it to a particular colour) and then using <code>document.querySelectorAll</code> or some selector library to select elements with that colour and then apply styling to them.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload