Making Selection & Adding tag dynamically in JavaScript

前端 未结 1 1906
天涯浪人
天涯浪人 2021-02-02 04:19

I need some help about JavaScript on iPhone UIWebView;
I have HTML like below:


   
      Example

        
相关标签:
1条回答
  • 2021-02-02 05:14

    Since this is a UIWebView, this is Mobile Safari and you can lose a lot of those branches for obtaining the selection. I would suggest using document.execCommand() with the "HiliteColor" command, which is built into the browser and works on the whole selection even when it crosses element boundaries:

    var sel = window.getSelection();
    if (!sel.isCollapsed) {
        var selRange = sel.getRangeAt(0);
        document.designMode = "on";
        sel.removeAllRanges();
        sel.addRange(selRange);
        document.execCommand("HiliteColor", false, "#ffffcc");
        sel.removeAllRanges();
        document.designMode = "off";
    }
    
    0 讨论(0)
提交回复
热议问题