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

后端 未结 3 1772
暗喜
暗喜 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条回答
  •  萌比男神i
    2021-01-13 02:53

    Try using .html() , String.prototype.slice(). To select "llo world" call .slice() with parameters 3, 12

    $("#textdiv").html(function(i, html) {
      var selected = html.slice(3, 12);
      return html.replace(new RegExp("("+selected+")"), "$1")
    })
    
    
    Hello world. I am a friend.

提交回复
热议问题