最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - HTML Content Editable div: select text event - Stack Overflow

programmeradmin0浏览0评论

I have a div which is contenteditable

<div class="editable" contenteditable="true"></div>

The user can enter any content in there. Is there any way to get the event for when a user makes a selection within the div.

Something like:

$('.editable').onSelection(function(e, selection){alert(selection);}

I have a div which is contenteditable

<div class="editable" contenteditable="true"></div>

The user can enter any content in there. Is there any way to get the event for when a user makes a selection within the div.

Something like:

$('.editable').onSelection(function(e, selection){alert(selection);}
Share Improve this question edited Feb 4, 2020 at 8:43 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Nov 8, 2012 at 9:13 unniunni 2,9914 gold badges21 silver badges22 bronze badges 2
  • possible duplicate of Selected text event trigger in Javascript :) might help! – Tats_innit Commented Nov 8, 2012 at 9:16
  • @Tats_innit thanks for the pointer. But stackoverflow.com/a/11786495/1008421 kind of solves the problem. But I would like to if there is some other better way to do this – unni Commented Nov 8, 2012 at 9:31
Add a comment  | 

3 Answers 3

Reset to default 13

you could try something like this:

There is no 'selectend' event but we can work out when the user has finished selecting by watching the mouseup event

$(function () {
    $('.editable').on('selectstart', function () {
        $(document).one('mouseup', function() {
            alert(this.getSelection());
        });
    });
});​

Following the answer by @Elliot and the comment by @Michael Bates, this seems to work flawlessly for both mouse and keyboard selection events (example is in TypeScript):

export function attachSelectionListener(element: HTMLElement) : void {
  if (!element.contentEditable) {
    return;
  }
  element.onselectstart = () => handleSelectionChange(element);
}

function handleSelectionChange(element: HTMLElement): void {
  document.onmouseup = () => retrieveSelection(element);
  document.onkeyup = () => retrieveSelection(element);
}

function retrieveSelection(element: HTMLElement) : void {
  const selection = document.getSelection();

  // Ignore empty selection
  if (!selection || !selection.toString()) {
    return;
  }
  alert(selection.toString());
}

When using this in your app, you probably want to check if you need to remove the listeners again at some point.

$('#ta').select(function() {
alert('Handler for .select() called.');
});

.select( handler(eventObject) ) Returns: jQuery Bind an event handler to the "select" JavaScript event, or trigger that event on an element.

.select( handler(eventObject) ) handler(eventObject)A function to execute each time the event is triggered.

.select( [eventData], handler(eventObject) ) eventDataA map of data that will be passed to the event handler. handler(eventObject)A function to execute each time the event is triggered.

check link

发布评论

评论列表(0)

  1. 暂无评论