服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

用javascirpt在中插入元素(光标位置),大家看看!


  html>
<head>    
<script>    
function storecaret (textel) {
if (textel.createtextrange)
textel.caretpos = document.selection.createrange().duplicate();    
}
function insertatcaret (textel, text) {
if (textel.createtextrange && textel.caretpos) {
var caretpos = textel.caretpos;
caretpos.text =caretpos.text.charat(caretpos.text.length - 1) == ' ' ?text + ' ' : text;      
}      
else        
textel.value  = text;
    
}    
</script>    
</head>    
<body>    
<form name="aform">
<textarea name="atextarea" rows="5" cols="80" wrap="soft"  onselect="storecaret(this);" onclick="storecaret(this);" onkeyup="storecaret(this);">  这是例子,你可以在这里添加文字、插入文字。 </textarea>    
<br>  
<input type="text" name="atext" size="80" value="我要在光标处插入这些文字"><br>
<input type="button" value="我要在光标处插入上面文本框里输入的文字!"  onclick="insertatcaret(this.form.atextarea, this.form.atext.value);">
</form>
</body>
</html> 

扫描关注微信公众号