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

标签 javascript jquery css regex

我正在尝试编写一个程序,当我在一个 div 中选择一个文本时,我想突出显示该 div 中所有出现的选定文本,我到目前为止所做的一切

Highlights the selected text anywhere in that div

但这只适用于静态的,即硬编码的单词,如演示中所示 像这样

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

这里 Ipsum 是硬编码的,它工作正常。我想做的是用动态选择的文本替换 ipsum,但失败了。我已经添加了我到目前为止所达到的演示,下面给出了代码 Demo getting selected text dynamically on mouseup

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" />

查询

$("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

.red {
color: red;
}

最佳答案

DEMO

我正在使用此函数获取选定的文本

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;
}

然后我使用这个脚本来突出显示文本

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);
    });
}

因为这是一个函数,您可以将它用于任何您想要响应突出显示的div

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

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>

关于javascript - 用鼠标选择文本并突出显示该 div 中多次出现的相同文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23881509/

相关文章:

javascript - 你如何在 jQuery 的 document.ready 中使用揭示模块模式?

jquery - 居中幻灯片图像

带有 Asp.net 母版页的 Jquery 验证插件无法正常工作

javascript - 更改组合框中第一个滚动中可见的元素数

html - 为什么我们应该为特定浏览器定义 border-radius ?

javascript - Jquery排序表 ->文本前的数字

javascript - 使用 @auth0/angular-jwt 进行基于 Angular 5 角色的身份验证

javascript - react 从子元素到父元素的状态绑定(bind)

javascript - CSS "overflow-x:hidden"与浏览器冲突 Ctrl+F 水平移出屏幕文字高亮

iphone - iPhone 上的文字颤抖