iframe editer focus on

來源:互聯網
上載者:User
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
  5. <script type="text/javascript">
  6. <!--
  7.    var currentPos = 0,edit;  
  8.    window.onload=function(){
  9.        edit=$("fzone").contentWindow;
  10.        edit.document.designMode = "on";
  11.        edit.document.contentEditable = true;
  12.        edit.document.open();
  13.        edit.document.write("123456789");
  14.        edit.document.close();
  15.        $("inp").onclick=function(){
  16.            setCaret(edit.document.body);
  17.        }
  18.        edit.document.onclick=function(){
  19.            getPos(this);
  20.            //alert("test")
  21.        }
  22.    }
  23.    function setCaret(obj){
  24.        var rng = obj.createTextRange();
  25.        rng.collapse(true);
  26.        rng.moveStart('character',currentPos);
  27.        rng.select();
  28.    } 
  29.    function getPos(obj){    
  30.        obj.focus();
  31.        var workRange=edit.document.selection.createRange();
  32.        var allRange=edit.document.body.createTextRange(); //edit.document.selection.createRange()改為edit.document.body.createTextRange()
  33.        workRange.setEndPoint("StartToStart",allRange);
  34.        var len=workRange.text.length;
  35.        workRange.collapse(false);
  36.        workRange.select();
  37.        currentPos=len;
  38.        //alert(len);
  39.    }
  40.    function $(oid){
  41.        return document.getElementById(oid);
  42.    }
  43. //-->
  44. </script> 
  45. </head>
  46. <body>
  47.    <iframe id="fzone" style="width:400px;height:250px;"></iframe>
  48.    <br />
  49.    <input type="button" unselectable="on" value="Click" id="inp" />
  50. </body>
  51. </html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.