Selecting text in an element (akin to highlighting with your mouse)

后端 未结 16 2019
孤街浪徒
孤街浪徒 2020-11-21 05:58

I would like to have users click a link, then it selects the HTML text in another element (not an input).

By \"select\" I mean the same way you would select

16条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-21 06:16

    This thread (dead now) contains really wonderful stuff. But I'm not able to do it right on this page using FF 3.5b99 + FireBug due to "Security Error".

    Yipee!! I was able to select whole right hand sidebar with this code hope it helps you:

        var r = document.createRange();
        var w=document.getElementById("sidebar");  
        r.selectNodeContents(w);  
        var sel=window.getSelection(); 
        sel.removeAllRanges(); 
        sel.addRange(r); 
    

    PS:- I was not able to use objects returned by jquery selectors like

       var w=$("div.welovestackoverflow",$("div.sidebar"));
       
       //this throws **security exception**
    
       r.selectNodeContents(w);
    

提交回复
热议问题