简体   繁体   English

将div附加到选定的文本

[英]Append div to selected text

I want to show some buttons if someone select text in a editable area - like this: 如果有人在可编辑区域中选择文本,我想显示一些按钮 - 如下所示:

在此输入图像描述

So, I think I have to use <div contenteditable="true"> instead of <textarea> 所以,我想我必须使用<div contenteditable="true">而不是<textarea>

But to be honest I have no idea how to append the div 但说实话,我不知道如何追加div

Questions: 问题:

  1. Whats the (jquery) event for selecting text? 是什么(jquery)事件选择文本?
  2. How to append a div after select to this area? 选择此区域后如何附加div?

JsFiddle: http://jsfiddle.net/hA7Zn/ JsFiddle: http//jsfiddle.net/hA7Zn/

Thanks for any hints! 谢谢你的任何提示!

Selecting the text: 选择文字:

var text = $('text_element').text();

Selecting the html: 选择html:

var html = $('text_element').html()

Appending data: 追加数据:

$('appending_element').append(text)

If you want to append the div in the end of your div, use .append() . 如果要将div附加到div的末尾,请使用.append()

OR 要么

If you want to add your div after some particular div, use .insertAfter() . 如果要在某个特定div之后添加div,请使用.insertAfter()

Check this Demo for getting selected text, 检查此演示以获取所选文本,

Event selectstart is fired in case of selection 如果选择,将触发事件selectstart

$(function () {
    $('div').on('selectstart', function () {
        console.log('..');
        $(document).one('mouseup', function() {
            alert(this.getSelection());
        });
    });
});

May be this code will help you:- 可能是这段代码会帮助你: -

<textarea cols="45" rows="5" id="message">
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
</textarea>
<script>

$("textarea").click(function(){
    var txt=document.getElementById("message");
    alert(txt.value.substr(txt.selectionStart, (txt.selectionEnd -txt.selectionStart)));
});
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM