用js控制按鈕時間,將指定內容添加到文本域的游標所在位置。

來源:互聯網
上載者:User

標籤:js   javascript   cookie   表單   文本域   

<pre class="javascript" name="code">
var fm = document.getElementsByTagName('form')[0];
var button = document.getElementsByName("button");
//擷取一個cookie的值function getCookie(index){var allcookies = document.cookie;  var cookie_pos = allcookies.indexOf(index);if (cookie_pos != -1){cookie_pos += index.length + 1;var cookie_end = allcookies.indexOf(";",cookie_pos); if (cookie_end == -1){    cookie_end = allcookies.length;  }  var value = unescape(allcookies.substring(cookie_pos, cookie_end)); }    return value;  }//擷取文本中的游標位置 function getCursortPosition (ctrl) {var CaretPos = 0;// IE Supportif (document.selection) {ctrl.focus ();var Sel = document.selection.createRange ();Sel.moveStart ('character', -ctrl.value.length);CaretPos = Sel.text.length;}else if (ctrl.selectionStart || ctrl.selectionStart == '0')CaretPos = ctrl.selectionStart;return (CaretPos);}


 
//擷取文本游標位置並設定一個cookie來儲存游標時時變動的位置的函數var textpos = 0;textcontent.onclick  = function(){textpos = getCursortPosition (textcontent);document.cookie="textpos="+textpos;textpos = getCookie("textpos");}//字串的寫入函數function content(string){s = textcontent.value;fm.content.value  = s.substring(0,getCookie("textpos"))+string+s.substring(getCookie("textpos"));}
<pre class="php" name="code">//點擊按鈕向表單中的文本域的游標處新增內容button[0].onclick  = function(){content('[b][/b]');}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.