简体   繁体   English

如何替换window.getSelection()。toString()数据

[英]How to replace window.getSelection().toString() data

I have gotten the window.getSelection() data that the user has selected. 我已经获得了用户选择的window.getSelection()数据。 How to replace or update the current selected data? 如何更换或更新当前选定的数据? I want to replace currently selected data with what is in the variable 我想用变量中的内容替换当前选择的数据

$scope.mouseUpEvent = function() {

    $scope.selectedText =  $scope.getSelectionText();
   // do i do the function here to replace

}; 

$scope.getSelectionText = function(){

// Predefine select and range
    var sel, range;
    if (typeof window.getSelection != "undefined") {
    sel = window.getSelection();
        if (sel.rangeCount) {
           var range = sel.getRangeAt(0),
            content = range.cloneContents();
     }
 }

HTML HTML

 <div ng-mouseup="mouseUpEvent()"   ng-keyup="keyUpEvent()" contenteditable >Lorem ipsum dolor sit amet, consectetur</div>

To replace an element in javascript you first need to create the element using document.createElement and than you need to insert the created element into the range that you have predefiened. 要替换javascript中的元素,首先需要使用document.createElement创建元素,而不是需要将创建的元素插入到已预先定义的范围中。

 createEl= document.createElement(div);
 range.insertNode(createEl);

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

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