简体   繁体   English

我如何为window.getSelection值分配文本方向RTL,我试过但是如何从文本区域的现有值中删除getSelection

[英]how to i assign text direction RTL for window.getSelection value, I tried but how to remove getSelection from existing value of text area

 var F1 = document.getElementById('samuText').value; function insertAtCursor() { if (window.getSelection) { sel = window.getSelection(); document.getElementById('samuText').value = F1+sel // here i need to remove selected value from F1 //alert(sel); //ocument.getElementById(sel).style.direction = "rtl"; } } 
 <textarea id="samuText">Samudrala Ramu</textarea><button onclick="insertAtCursor();">RLM</button> 

Okay you should be able to get the selected text and its index(text position). 好的,您应该能够获取所选文本及其索引(文本位置)。 I searched and didn't find any solutions to get the text position from <textarea> . 我进行了搜索,但没有找到任何方法来从<textarea>获取文本位置。 But this answer provided a way to get the text position from <div> . 但是此答案提供了一种从<div>获取文本位置的方法。 And you can make <div> look like <textarea> . 您可以使<div>看起来像<textarea> I combined those two answers and slightly modified. 我结合了这两个答案并稍加修改。 Note that changeValue function is the key for manipulating text. 注意changeValue函数是操作文本的关键。

<html>
<style>
#main {
    -moz-appearance: textfield-multiline;
    -webkit-appearance: textarea;
    border: 1px solid gray;
    font: medium -moz-fixed;
    font: -webkit-small-control;
    height: 28px;
    overflow: auto;
    padding: 2px;
    resize: both;
    width: 400px;
}
</style>

<body>
<div id="main" contenteditable>Samudrala RamuSamu</div>
<input type="button" onclick="changeValue()" unselectable="on" value="Get selection">
</body>

<script>
function getSelectionCharOffsetsWithin(element) {
    var start = 0, end = 0;
    var sel, range, priorRange;
    if (typeof window.getSelection != "undefined") {
        range = window.getSelection().getRangeAt(0);
        priorRange = range.cloneRange();
        priorRange.selectNodeContents(element);
        priorRange.setEnd(range.startContainer, range.startOffset);
        start = priorRange.toString().length;
        end = start + range.toString().length;
    } else if (typeof document.selection != "undefined" &&
            (sel = document.selection).type != "Control") {
        range = sel.createRange();
        priorRange = document.body.createTextRange();
        priorRange.moveToElementText(element);
        priorRange.setEndPoint("EndToStart", range);
        start = priorRange.text.length;
        end = start + range.text.length;
    }
    return {
        start: start,
        end: end,
        value: range.toString()
    };
}

function changeValue() {
    var mainDiv = document.getElementById("main");
    var sel = getSelectionCharOffsetsWithin(mainDiv);

    var mainValue = mainDiv.textContent;
    var newValue = mainValue.slice(0,sel.start) + mainValue.slice(sel.end) + sel.value;
    mainDiv.textContent = newValue;
}
</script>

</html>

暂无
暂无

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

相关问题 如何使用html标记从window.getSelection()。getRangeAt(0)中包装文本选择? - How do I wrap a text selection from window.getSelection().getRangeAt(0) with an html tag? 使用window.getSelection,我怎么知道会影响来自两个不同节点的文本? - With window.getSelection, how do I know affect text from two different nodes? 如何让window.getselection用于输入type = text字段 - How do I get window.getselection to work for an input type=text field window.getSelection() 给了我选定的文本,但我想要 HTML - window.getSelection() gives me the selected text, but I want the HTML 如何在 ReactJS 中获取突出显示的文本 (window.getSelection()) - How can you get highlighted text (window.getSelection()) in ReactJS 在从Z4C4C4AD5FCA2E7A3F74DBB1CED00381AA4Z段落中选择文本上,使用Z05B8C74CBD96FBD96FBF2DE4C1A352702FBF4Z.getSelection() - On selecting text from HTML paragraph using window.getSelection(), the range index value is giving 0 when text from different/child tag is selected window.getSelection()的值在回调期间更改 - Value of window.getSelection() changes during callback 检查window.getSelection中的值是否与字符串匹配 - Checking if a value from window.getSelection matches a string 无法从window.getselection获取任何值 - Unable to get any value from window.getselection 如何替换window.getSelection()。toString()数据 - How to replace window.getSelection().toString() data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM