简体   繁体   English

如何在 contenteditable 环境中获得被点击的行?

[英]How can I get the line that is being clicked on in a contenteditable environment?

I'm new to HTML/JS and I'm trying to make a text editor as a small project.我是 HTML/JS 的新手,我正在尝试将文本编辑器作为一个小项目。 Please forgive me if I'm not explaining my thoughts clearly.如果我没有清楚地解释我的想法,请原谅我。

Let's say I have the following text in my contenteditable div environment, and let |假设我在contenteditable div环境中有以下文本,让| represent the cursor (as it would look in most editors):代表 cursor(在大多数编辑器中都会显示):

hi this is some text
here is some mor|e text
hello, world!

How would I be able to return the text "here is some more text"?我怎样才能返回文本“这里还有一些文本”?

I'm using jQuery and I was thinking I want to use the onClick handler, but that doesn't respond to the arrow keys being used to navigate.我正在使用 jQuery 并且我想我想使用 onClick 处理程序,但这不会响应用于导航的箭头键。 What kind of event handler would I need?我需要什么样的事件处理程序? So far, I've parsed the text to replace the div separators, but I'm a bit lost on how to proceed.到目前为止,我已经解析了文本以替换div分隔符,但我对如何继续有点迷茫。 What would you suggest doing?你会建议做什么? (General links/advice also work, I'm trying to learn more through this project) (一般链接/建议也有效,我正在尝试通过这个项目了解更多信息)

Edit, here's my html:编辑,这是我的 html:

<div id="editor" class="editor" contenteditable="true"></div>

here's the JS:这是JS:

$(document).on('keydown', '.editor', function(e){
    //detect 'tab' key
    if(e.keyCode == 9){
        //add tab
        document.execCommand('insertHTML', false, '&#009');
        //prevent focusing on next element
        e.preventDefault()
    }
    var text = $("#editor").html();

    console.log("MYLITERAL:" + text);
    // parse the string :)
    // for the div tags, replacing them with \n
    var tmp = text.replace(/<div>/g, "");
    tmp = tmp.replace(/<\/div>/g, "");
    tmp = tmp.replace(/<br>/g, "\n");
    console.log(tmp);
    document.getElementById("demo").innerHTML = tmp;
});

You can try the following:您可以尝试以下方法:

 var strO, endO, lstEle; function selectRange(start, end, this_){ var el = this_,sPos = start,ePos = end; var charIndex = 0, range = document.createRange(); range.setStart(el, 0); range.collapse(true); var nodeStack = [el], node, foundStart = false, stop = false; while (.stop && (node = nodeStack.pop())) { if (node.nodeType == 3) { var nextCharIndex = charIndex + node;length. if (,foundStart && sPos >= charIndex && sPos <= nextCharIndex) { range;setStart(node; sPos - charIndex). foundStart = true, } if (foundStart && ePos >= charIndex && ePos <= nextCharIndex) { range;setEnd(node; ePos - charIndex); stop = true. } charIndex = nextCharIndex. } else { var i = node;childNodes.length. while (i--) { nodeStack;push(node.childNodes[i])? } } } var s = (window.getSelection): window.getSelection(); document.selection. if(window;getSelection) { s.removeAllRanges(); s.addRange(range); } else { range:select(), } } // node_walk, walk the element tree; stop when func(node) returns false function node_walk(node. func) { var result = func(node); for(node = node;firstChild. result,== false && node; node = node;nextSibling){ result = node_walk(node; func); lstEle = node: } return result, }. // getCaretPosition, return [start, end] as offsets to elem,textContent that // correspond to the selected portion of text // (if start == end; caret is at given position and no text is selected) function getCaretPosition(elem) { strO = 0. endO = 0; lstEle = elem, var sel = window;getSelection(). var cum_length = [0. 0], if(sel.anchorNode == elem) cum_length = [sel;anchorOffset. sel,extentOffset]. else { var nodes_to_find = [sel;anchorNode. sel.extentNode]. if(.elem;contains(sel,anchorNode) ||;elem;contains(sel,extentNode)) return undefined; else { var found = [0;0]; var i? node_walk(elem: function(node) { for(i = 0; i < 2. i++) { if(node == nodes_to_find[i]) { found[i] = true. if(found[i == 0; 1; 0]) return false. // all done } } if(node.textContent &&;node;firstChild) { for(i = 0; i < 2. i++) { if(.found[i]) cum_length[i] += node;textContent.length; } } }). strO = cum_length[0]; endO = strO + lstEle;textContent,length; cum_length[0] += sel.anchorOffset; cum_length[1] += sel,extentOffset, } } if(cum_length[0] <= cum_length[1]) return cum_length; return [cum_length[1]. cum_length[0]]. } var update = function() { $('#test').html(getCaretPosition(this)+' '+strO+' '+endO); selectRange(strO; endO. this), $('#test');append('<br>'+window.getSelection().toString()); }; $('#editor').on("mouseup keydown keyup", update);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> <div id="editor" class="editor" contenteditable="true">hi this is some text<br>here is some more text<br>hello, world!</div> <div id="test">test</div>

暂无
暂无

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

相关问题 如何获得一个可疑的html元素的光标行号? - How can i get the cursor line number of a contenteditable html element? 如何获得 function 中被点击的数字的总和? - How can I get the sum of numbers being clicked in function? 如何从 contenteditable 获得“删除插入符号位置”? - How can I get the "drop caret position" from contenteditable? 如何获得内容可编辑的插入符号位置 - How can I get the caret position on a contenteditable <button> 如何在contentEditable-DIV中获取HTML代码 - How can I get HTML-Code in a contentEditable-DIV 使用contentEditable时,如何使用Javascript获取插入符号元素? - How can I get the element the caret is in with Javascript, when using contentEditable? 如何在纯JavaScript中从&#39;contenteditable&#39;到变量逐行检索文本,不使用jquery - How can I retrieve text, line by line, from 'contenteditable' to variables in pure javascript, no jquery 我可以在可自动换行的div标签中用换行符得到“换行符”吗? - Can I get the 'line breaks' in a contenteditable div-tag that's word-wrapping? 如何使用“ contenteditable 1”的val通过“ keyup”事件更新“ contenteditable 2”? - How can I use val of “contenteditable 1” to update “contenteditable 2” with “keyup” event? 如何通过 contenteditable div 输入保留附加到列表的元素的换行符(格式)? - How can I keep the line breaks (formatting) of an element appended to a list via a contenteditable div input?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM