Starting a Javascript Trigger

Can I start the start selection through javascript / jQuery?

In my opinion, the choice of mouse text can be divided into the following steps:

1) raise the mousedown event and start the selection

2) run mousemove and move the "endpoint" of the selection

3) call the mouse and stop the selection

Only step 1) can be called, i.e. the text in the span element ... ... so after that the moving mouse cursor will behave like a selection waiting for the mouse?

Or , how is it implemented and how does the browser know that the choice is starting?

Rangy library: I know this javascript library for creating custom range selection. But it seems too complicated for me to imitate my own choice through mouse events. I just want to tell the browser that I started choosing in (span, offset 2), and it should continue in step .

+4
source share
1 answer

You can use the mouse events on the selectable item and document to control when you want to start and end the selection, and when you want to check the selection, etc.

Then during the selection, you can use window.getSelection() to get information about the current selection.

Try something like this:

 var m_MouseDown = false; document.getElementById('selectMyContent').onmousedown = function (e) { m_MouseDown = true; }; document.onmouseup = function (e) { m_MouseDown = false; }; document.onmousemove = function(e) { if (m_MouseDown) { document.getElementById('showTextHere').innerHTML = getSelectionInfo(); } } function getSelectionInfo() { var selected = ""; if (typeof window.getSelection != "undefined") { var sel = window.getSelection(); if (sel.rangeCount) { for (var i = 0, len = sel.rangeCount; i < len; ++i) { var range = sel.getRangeAt(i); var txt = document.createElement('div'); txt.appendChild(range.cloneContents()); selected = range.startContainer.parentNode.id + ':' + range.startOffset + '-' + range.endOffset + ' "' + txt.innerHTML + '"'; } } } return selected; } 

See http://jsfiddle.net/nY328/10/ for an example.

+1
source

Source: https://habr.com/ru/post/1444364/


All Articles