点击按钮插入文字在文本框
2014-08-01 10:22
267 查看
一款实用的网页特效,点击按钮在文本框中插入文字,特别是在一些需要输入文字的地方尤其适用,可减少用户输入的麻烦,只需点击一下对应的按钮,即可把文字插入到文本框的指定位置,不过目前来说,本代码不算完善,有一些小的Bug希望高手们帮忙修正哦。
<!doctype html> <html> <head> <title>点击按钮插入文字</title> <script language="javascript"> //移动光标到最后 var setPos=function(o){ if(o.setSelectionRange){//W3C setTimeout(function(){ o.setSelectionRange(o.value.length,o.value.length); o.focus(); },0); }else if(o.createTextRange){//IE var textRange=o.createTextRange(); textRange.moveStart("character",o.value.length); textRange.moveEnd("character",0); textRange.select(); } }; function Insert(str) { var obj = document.getElementById('content'); setPos(obj); if(document.selection) { obj.focus(); var sel=document.selection.createRange(); document.selection.empty(); sel.text = str; } else { var prefix, main, suffix; prefix = obj.value.substring(0, obj.selectionStart); main = obj.value.substring(obj.selectionStart, obj.selectionEnd); suffix = obj.value.substring(obj.selectionEnd); obj.value = prefix + str + suffix; } obj.focus(); } </script> </head> <body> <table width="630" height="55" border="0" cellpadding="0" cellspacing="0"> <tr><td> <label> <textarea id="content" name="textarea" style="width:500px; height:120px;"></textarea> </label> </td></tr> <tr> <td width="567" align="left" > <input type="button" style="cursor:hand" onclick="javascript:Insert('急')" value="急 " /> <input type="button" style="cursor:hand" onclick="javascript:Insert('发')" value="发 " /> <input type="button" style="cursor:hand" onclick="javascript:Insert('求')" value="求 " /> <input type="button" style="cursor:hand" onclick="javascript:Insert('回')" value="回 " /> <input type="button" style="cursor:hand" onclick="javascript:Insert('货')" value="货 " /> <input type="button" style="cursor:hand" onclick="javascript:Insert('至')" value="至 " /> <input type="button" style="cursor:hand" onclick="javascript:Insert('车')" value="车 " /> <input type="button" style="cursor:hand" onclick="javascript:Insert('到')" value="到 " /> <input type="button" style="cursor:hand" onclick="javascript:Insert('要')" value="要 " /> <input type="button" style="cursor:hand" onclick="javascript:Insert('在')" value="在 " /> <input type="button" style="cursor:hand" onclick="javascript:Insert('剩')" value="剩 " /> <input type="button" style="cursor:hand" onclick="javascript:Insert('空')" value="空 " /> <input type="button" style="cursor:hand" onclick="javascript:Insert('装')" value="装 " /> <input type="button" style="cursor:hand" onclick="javascript:Insert('占')" value="占 " /> <input type="button" style="cursor:hand" onclick="javascript:Insert('或')" value="或 " /> <input type="button" style="cursor:hand" onclick="javascript:Insert('节')" value="节 " /> <input type="button" style="cursor:hand" onclick="javascript:Insert('大')" value="大 " /> <input type="button" style="cursor:hand" onclick="javascript:Insert('小')" value="小 " /> <input type="button" style="cursor:hand" onclick="javascript:Insert('卸')" value="卸 " /> </td> </tr> </table> </body> </html>
相关文章推荐
- 点击按钮实现复制文本框中的文字 …
- 黑马程序员之WinForm编程基础学习笔记:页面上有一个文本框,文本框左侧和右侧各有一个按钮,点击左测按钮文本框中的文字向左循环滚动一次,点击右侧按钮文本框中的文字向右循环滚动一次。
- div小型浮动层,类似锚点提示文字,鼠标点击按钮显示/隐藏
- 点击按钮弹出文本框
- 点击文本框选中里面的文字
- 点击键盘上的“Next”按钮实现文本框焦点跳转
- 点击一个切换按钮 按钮的文字切换 对应的内容也跟着显示隐藏
- 点击input文本框,文字消失 JS
- Jquery实现点击文字后变成文本框且可修改
- CSS小例子(只显示下划线的文本框,像文字一样的按钮)
- iOS 点击键盘上的“Next”按钮实现文本框焦点跳转
- Android Button点击效果(按钮背景变色、文字变色)
- input文本框、文字、按钮、图片 垂直居中对齐的解决办法
- JavaScript 点击插入文字
- 使用Jquery实现点击文字变成输入框、点击按钮刷新网页
- Android Button按钮点击背景和文字变化
- iOS 点击键盘return按钮 文本框UiTextField 自动换行
- jQuery实现按钮的点击 全选/反选 单选框/复选框 文本框 表单验证
- JS实现点击按钮实现文字变大变小
- html中input文本框,初始里边有文字提示,当点击输入文字时,原文字消失