简体   繁体   English

jQuery:在鼠标单击位置拆分文本

[英]jQuery: Split text at mouse-click position

I'm trying to program something like a textarea, just for training. 我正在尝试编写类似textarea的东西,仅用于训练。

I have a div with (user-generated) text in it: 我有一个带有(用户生成的)文本的div:

<div id="editor">Hello! I am a Text!</div>

If I click (with left mousebutton) between the two "L" of "Hello", for example, I'd like to see this: 例如,如果我在“Hello”的两个“L”之间单击(使用鼠标左键),我希望看到:

<div id="editor">Hel<div id="cursor">|</div>lo! I am a Text!</div>

How do I achieve this? 我该如何实现这一目标? Until now, I'm doing it with... 到现在为止,我正在做...

//HTML
<div id="editor" onclick="setCursorWithMouse()"><!-- User-Text --></div>

//jQuery
function setCursorWithMouse(){
    $('#editor').append('<div id="cursor">|</div>');
}

...but, of course, it just adds the cursor at the end of the div. ...但是,当然,它只是将光标添加到div的末尾。

How can I know where exactly the user clicked, where to add my cursor (or whatever), where to split the text? 如何知道用户点击的确切位置,添加光标(或其他)的位置,文本分割位置?

Thanks 谢谢

Edit: 编辑:

  • I do not want to use contenteditable! 我不想用contenteditable! Just look at it this way: I want to split a string at the mouse-click position. 只需这样看:我想在鼠标点击位置拆分一个字符串。

  • I know .append() is not what I'm looking for! 我知道.append()不是我想要的! .append() was just a Placeholder for a later, better, function. .append()只是一个占位符,用于以后更好的功能。 THIS function I'm right now asking for. 这个功能我现在正在要求。

  • I use a fixed-width font. 我使用固定宽度的字体。

this is as close as I could get: DEMO 这是我能得到的: DEMO

var clicked=false;
$('#editor').click(function(e){
    $(this).addClass('active');
    var letterWidth=7;
    $('#editor span').remove();
    var clickPos=e.pageX-$(this).offset().left;
    if(!clicked) clickPos+letterWidth;
    var letter=Math.round(clickPos/letterWidth);
    var before=$(this).html().substr(0,letter);
    var after=$(this).html().substr(letter,$(this).html().length);
    $(this).html(before+'<span>|</span>'+after);
    clicked=true;
});
$(document).click(function(e){
    if(!$('#editor').is(e.target)){
        $('#editor').removeClass('active');
        $('#editor span').remove();
        clicked=false;
    }
});

I hope it's close enough! 我希望它足够接近! :) :)

NOTE: 注意:

this code is written assuming that the font-size is 16px ! 假设font-size16px ,编写此代码!

I'm afraid you have to calculate the font-size for further expansions. 我担心你必须计算font-size才能进一步扩展。

Note, does not return expected results at firefox ; 注意,不会在firefox上返回预期的结果; chrome returns expected results , though cursor may move to either left or right by 1 character , if mouse not released slowly. chrome返回预期结果,但如果鼠标没有缓慢释放,光标可能会向左或向右移动1个字符。

 $(function() { $(document).data("text", $("#editor").text()) .on("mousedown", function() { $("#editor") .html($(this) .data("text")) }) .on("mouseup", function() { that = $("#editor"); var sel = window.getSelection ? window.getSelection() : document.selection(); var o = that.text(); var before = sel.baseOffset; var after = o.length - before; var a = o.slice(0, before); var b = after === 0 ? "" : o.slice(-after); var n = "<data>|</data>"; var html = (after === "" ? a + n : a + n + b); that.html(html); }); }) 
 #editor { font-family: Sans; font-size: 28px; letter-spacing: 8px; white-space: pre; } #editor > data { color: red; max-width: .1em; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div tabindex="0" id="editor">Hello! I am a Text!</div> 

jsfiddle http://jsfiddle.net/guest271314/maakff7q/ jsfiddle http://jsfiddle.net/guest271314/maakff7q/

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

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