HTML内容可编辑div:选择文本事件

时间:2022-12-09 15:42:36

I have a div which is contenteditable

我有一个令人满意的div

<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.

用户可以在其中输入任何内容。当用户在div中进行选择时,有没有办法获取事件。

Something like:

就像是:

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

2 个解决方案

#1


12  

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

没有'selectend'事件,但是当用户通过观看mouseup事件完成选择时我们可以解决

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

#2


-2  

$('#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))返回:jQuery将事件处理程序绑定到“select”JavaScript事件,或在元素上触发该事件。

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

.select(handler(eventObject))handler(eventObject)每次触发事件时执行的函数。

.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.

.select([eventData],handler(eventObject))eventDataA将传递给事件处理程序的数据映射。 handler(eventObject)每次触发事件时执行的函数。

check link

检查链接

#1


12  

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

没有'selectend'事件,但是当用户通过观看mouseup事件完成选择时我们可以解决

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

#2


-2  

$('#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))返回:jQuery将事件处理程序绑定到“select”JavaScript事件,或在元素上触发该事件。

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

.select(handler(eventObject))handler(eventObject)每次触发事件时执行的函数。

.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.

.select([eventData],handler(eventObject))eventDataA将传递给事件处理程序的数据映射。 handler(eventObject)每次触发事件时执行的函数。

check link

检查链接