javascript控制在光标位置插入文字适合表情的插入
在实现表情的插入时用到了。
<spanstyle="font-size:18px;"><html>
<head>
<scripttype='text/javascript'>
functiontest(str){
vartc=document.getElementById("mytextarea");
vartclen=tc.value.length;
tc.focus();
if(typeofdocument.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>
<textarearows=5name=s1cols=27id="mytextarea">目的通过点击页面上的按钮button在textarea中的光标停留处插上文字</textarea>
<inputtype=buttononclick="test('这是需要加入的文字')"/>
</body>
</html>
</span>