+ -
当前位置:首页 → 问答吧 → 插入表情到光标处问题

插入表情到光标处问题

时间:2010-05-27

来源:互联网

为什么我使用:editor.focus();还是不行啊??
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <script language ="javascript" type="text/javascript"> function InsertPic(id,picpath){ var editor = document.getElementById(id); var img = document.createElement('img'); img.src = picpath; editor.focus(); editor.appendChild(img); } </script> </head> <body> <div> <img src="http://www.sinaimg.cn/uc/myshow/blog/misc/gif/E___0088ZHYXSIGT.gif" style="cursor:hand;" onClick="InsertPic('txtContent',this.src);" /><img src="http://www.sinaimg.cn/uc/myshow/blog/misc/gif/E___0177ZHTDSIGT.gif" style="cursor:hand;" onClick="InsertPic('txtContent',this.src);" /><img src="http://www.sinaimg.cn/uc/myshow/blog/misc/gif/E___0104ZHWSSIGT.gif" style="cursor:hand;" onClick="InsertPic('txtContent',this.src);" /></div> <textarea id="txtContent" cols="68" rows="8" runat="server">向textarea插入图片:</textarea> </body> </html>
 提示:您可以先修改部分代码再运行

作者: wangsdong   发布时间: 2010-05-27

editor.focus()是聚焦该文本框,并不能指定你图片插到什么地方,要是需要获取光标下的位置要用
document.selection.createRange(),然后一堆
没记,因为这是 IE only 的,其它浏览器下选择光标位置不知道..

作者: aolu11   发布时间: 2010-05-27

引用:
原帖由 aolu11 于 2010-5-27 11:09 发表
editor.focus()是聚焦该文本框,并不能指定你图片插到什么地方,要是需要获取光标下的位置要用
document.selection.createRange(),然后一堆
没记,因为这是 IE only 的,其它浏览器下选择光标位置不知道..
然后一堆???????

作者: wangsdong   发布时间: 2010-05-27

http://topic.csdn.net/u/20070626 ... b0e3.html?309844753
看下吧,不过我测试是 IE only 的,FF不行

作者: aolu11   发布时间: 2010-05-27

还是不明白啊。

作者: wangsdong   发布时间: 2010-05-27

都吃饭去了吧。。继续等待高手的出现。。

作者: wangsdong   发布时间: 2010-05-27

火狐的看这里:
https://developer.mozilla.org/en/DOM/TextArea

ie的看这里:
http://msdn.microsoft.com/zh-cn/library/ms535872%28en-us,VS.85%29.aspx

作者: baishui   发布时间: 2010-05-27

回7楼的。不好意思。我是英语盲啊。

作者: wangsdong   发布时间: 2010-05-27

找到了点代码。。怎么整合到我的那个里面去啊?
复制内容到剪贴板
代码:
var text=$("bcms_content");
        var textLength=text.value.length;
            text.focus();
        //判断浏览器
        if(typeof document.selection!="undefined")
        {
            //IE
            document.selection.createRange().text='[em='+id+']'
        }else{
            //FF兼容
            text.value=text.value.substr(0,text.selectionStart)+'[em='+id+']'+text.value.substring(text.selectionStart,textLength);
        }

作者: wangsdong   发布时间: 2010-05-27

自己顶下。。等待高手解决。。。

作者: wangsdong   发布时间: 2010-05-29

相关阅读 更多