Selecting text in a div programmatically using position values belong to that text

后端 未结 3 1745
暗喜
暗喜 2021-01-13 02:42

I have a div and there is a text in it.I want a part of that text to be selected programmatically according position value of characters.

3条回答
  •  再見小時候
    2021-01-13 03:10

    Code without any error checking ahead. Use at your own risk!

    function highlight(pattern) {
      
      var text = $("#textdiv").text();
      var around = text.split(pattern);
      $("#textdiv").html(around[0] + "" + pattern + "" + around[1]);
      
    }
    
    
    $(document).ready(function() {
    
      highlight("world");
      
      
    })
        
    .highlight {
      
      background-color: yellow;
      
    }
    
    
    Hello world. I'm Hugo!

提交回复
热议问题