javascript控制在光標(biāo)位置插入文字適合表情的插入
更新時(shí)間:2014年06月09日 10:06:22 作者:
使用javascript控制在光標(biāo)位置插入文字,在實(shí)現(xiàn)表情的插入時(shí)會(huì)用到的,需要的朋友可以參考下
直接上代碼吧,用js控制在光標(biāo)位置插入。
在實(shí)現(xiàn)表情的插入時(shí)用到了。
<span style="font-size:18px;"><html>
<head>
<script type='text/javascript'>
function test(str){
var tc = document.getElementById("mytextarea");
var tclen = tc.value.length;
tc.focus();
if(typeof document.selection != "undefined")
{
document.selection.createRange().text = str;
}
else
{
tc.value = tc.value.substr(0,tc.selectionStart)+str+tc.value.substring(tc.selectionStart,tclen);
}
}
</script>
</head>
<body>
<textarea rows=5 name=s1 cols=27 id="mytextarea">目的通過(guò)點(diǎn)擊頁(yè)面上的按鈕button 在textarea中的光標(biāo)停留處插上文字 </textarea>
<input type=button onclick="test('這是需要加入的文字')" />
</body>
</html>
</span>
在實(shí)現(xiàn)表情的插入時(shí)用到了。
復(fù)制代碼 代碼如下:
<span style="font-size:18px;"><html>
<head>
<script type='text/javascript'>
function test(str){
var tc = document.getElementById("mytextarea");
var tclen = tc.value.length;
tc.focus();
if(typeof document.selection != "undefined")
{
document.selection.createRange().text = str;
}
else
{
tc.value = tc.value.substr(0,tc.selectionStart)+str+tc.value.substring(tc.selectionStart,tclen);
}
}
</script>
</head>
<body>
<textarea rows=5 name=s1 cols=27 id="mytextarea">目的通過(guò)點(diǎn)擊頁(yè)面上的按鈕button 在textarea中的光標(biāo)停留處插上文字 </textarea>
<input type=button onclick="test('這是需要加入的文字')" />
</body>
</html>
</span>
相關(guān)文章
微信小程序自定義toast組件的方法詳解【含動(dòng)畫】
這篇文章主要介紹了微信小程序自定義toast組件的方法,結(jié)合實(shí)例形式詳細(xì)分析了微信小程序自定義toast組件的步驟、實(shí)現(xiàn)方法及相關(guān)操作技巧,需要的朋友可以參考下2019-05-05
javascript跟隨滾動(dòng)效果插件代碼(javascript Follow Plugin)
這篇文章介紹了javascript跟隨滾動(dòng)效果插件代碼(javascript Follow Plugin),有需要的朋友可以參考一下2013-08-08
解決echarts官網(wǎng)打不開訪問(wèn)失敗的問(wèn)題
這篇文章主要介紹了解決echarts官網(wǎng)打不開訪問(wèn)失敗的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
最好用的Bootstrap fileinput.js文件上傳組件
這篇文章主要為大家詳細(xì)介紹了最好用的Bootstrap fileinput.js文件上傳組件的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
點(diǎn)擊頁(yè)面任何位置隱藏div的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇點(diǎn)擊頁(yè)面任何位置隱藏div的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-09-09

