简体   繁体   English

使用鼠标选择文本并突出显示该 div 中多次出现的相同文本

[英]Selecting text with mouse and highlighting the same text with multiple occurences in that div

I am trying to code a program in which when i select a text inside a div on mouse up i want to highlight all the occurrence of the selected text in that div what i have done till now我正在尝试编写一个程序,当我在 select 上鼠标按下 div 中的文本时,我想突出显示该 div 中所有出现的选定文本我到目前为止所做的

Highlights the selected text anywhere in that div突出显示该 div 中任意位置的选定文本

But this works with only static ie hardcoded words as shown in the demo like this但这仅适用于 static 即硬编码字,如演示中所示

 var text = $('div').text().replace(/Ipsum/g,"<span class='red'>Ipsum</span>");

here Ipsum is hardcorded and it works fine.What i am trying to do is replace ipsum with dynamically selected text which fails.I have added the demo with what i have reached till now and the code is give below Demo getting selected text dynamically on mouseup这里 Ipsum 是硬编码的,它工作正常。我想做的是用动态选择的文本替换 ipsum,但失败了。我已经添加了我到目前为止所达到的演示,代码在下面给出了在 mouseup 上动态获取选定文本的演示

HTML HTML

<div id="div">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
 </div>

<input type="button" value="Click to remove highlight" id="id2" />

Jquery Jquery

$("div").mouseup(function(){
  $(this).after("Mouse button released.");
              var text = "";
    if (window.getSelection) {
        text = window.getSelection().toString();
    } else if (document.selection && document.selection.type != "Control") {
        text = document.selection.createRange().text;
    } var textspan ="<span class='red'>"+text+'</span>';
        var text1 = $('div').text().replace('/'+text+'/g',textspan);
        alert(text);
          alert(textspan);
            alert(text1);
$('div').html(text1);
});





$('#id2').click(
  function(){
      $( "span.red" ).each(function() {
  $( this ).contents().unwrap();

});

    }
);

Css Css

.red {
color: red;
}

Using contenteditable and execCommand .使用contenteditableexecCommand

 onmouseup = (function(){ document.execCommand("backColor", false, "chartreuse") window.getSelection().removeAllRanges() })
 <div contenteditable> In mathematics, extrapolation is the process of estimating, beyond the original observation range, the value of a variable on the basis of its relationship with another variable. It is similar to interpolation, which produces estimates between known observations, but extrapolation is subject to greater uncertainty and a higher risk of producing meaningless results. Extrapolation may also mean extension of a method, assuming similar methods will be applicable. Extrapolation may also apply to human </div>

DEMO演示

I am getting the selected text value using this function我正在使用此 function 获取选定的文本

function getSelectionText() {
    var text = "";
    if (window.getSelection) {
        text = window.getSelection().toString();
    } else if (document.selection && document.selection.type != "Control") {
        text = document.selection.createRange().text;
    }
    return text;
}

Then i am using using this script to highlight the text然后我使用这个脚本来突出显示文本

function thisRespondHightlightText(thisDiv){
    $(thisDiv).on("mouseup", function () {
        var selectedText = getSelectionText();
        var selectedTextRegExp = new RegExp(selectedText,"g");
        var text = $(this).text().replace(selectedTextRegExp, "<span class='red'>" + selectedText + "</span>");
        $(this).html(text);
    });
}

Since this is a function you can use it for any div you want to respond to highlighting .由于这是一个 function,您可以将它用于您想要响应突出显示的任何 div

thisRespondHightlightText(".select--highlight--active");

HTML: HTML:

<div class="select--highlight--active">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>

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

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