富文本编辑器,选区问题,求高手赐教

nmhyt 2013-02-20 10:27:46
利用iframe创建的富文本编辑器,其中两个功能思路暂无好的解决办法:
1.用户在编辑器中,选择文字,并选择特殊格式,如“副标题”,我则将所选区域的文字外围添加<div class='副标题对应的ClassName'></div>,execCommand也能勉强实现这个功能,但是每一次execCommand 都可能新增标签,如果当前的格式比较特殊,就很复杂,期待有别的思路或者好的解决办法
2.用户在回车之后,如果当前回车的这段文字(我的回车生成的div)首字是序号形式(1,2,3或者中文一,二,三),则将这个首字的样式修改为加粗。
现在的问题是:(a)获取选区文字,并替换,如果选区的字是重复的就不能实现,能不能获取到选区前后的位置,或者获取到选区所对应的parent父元素
(b)我怎么获取回车时前一段文字,比如所对应的div标签对象

因为是内部使用,暂时只需要兼容火狐和谷歌浏览器。
为什么最多只能给100分啊??
...全文
193 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
json6518507 2013-02-21
  • 打赏
  • 举报
回复
getCursorSelect: function(){ if (this.iframeDoc.selection) { this.selection = this.iframeDoc.selection; this.range = this.selection.createRange(); try { this.parent = this.range.parentElement(); } catch (e) { return; } } else { try { this.selection = this.iframe.contentWindow.getSelection(); } catch (e) { return; } this.range = this.selection.getRangeAt(0); this.parent = this.range.commonAncestorContainer; if(this.parent.nodeName == "#text") { this.parent = this.parent.parentNode; } } },
打字员 2013-02-20
  • 打赏
  • 举报
回复
以下是我寫的iframe編輯器獲取游標位置及選取對象的方法,你可以看看。 對你的兩個問題都有幫助 另外,你可以自己看看有關 Range對象的說明

getCursorSelect: function(){
		if (this.iframeDoc.selection) {
			this.selection = this.iframeDoc.selection;
			this.range = this.selection.createRange();
			try {
				this.parent = this.range.parentElement();
			} catch (e) {
				return;
			}
		} else {
			try {
				this.selection = this.iframe.contentWindow.getSelection();
			} catch (e) {
				return;
			}		
			this.range = this.selection.getRangeAt(0);
			this.parent = this.range.commonAncestorContainer;
			if(this.parent.nodeName == "#text") {
				this.parent = this.parent.parentNode;
			}
		}
	},

87,901

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧