在 IE 冲突中使用 createRange()
又来了。我一整天都在寻找这个问题的解决方案......
我得到了代码:
var formatString = function(t,style){
var el = $(t);
var pre;
var start;
var end;
var len;
var replace;
switch(style){
case 'italic':
pre = '[em]';
break;
case 'bold':
pre = '[b]';
break;
case 'paragraph':
pre = '[p]';
break;
}
if(document.selection){
el.focus();
var sel = document.selection.createRange();
// Finally replace the value of the selected text with this new replacement one
sel.text = pre + sel.text + pre.replace(/(\[)/,'[/');
} else {
len = el.val().length;
start = t.selectionStart;
end = t.selectionEnd;
sel = el.val().substring(start, end);
replace = pre + sel + pre.replace(/(\[)/,'[/');
el.val(el.val().substring(0,start) + replace + el.val().substring(end,len));
}
}
我正在做的是创建一个简单的所见即所得,只是为了向显然经过处理以显示正确的 html 标签的文本添加一些格式。当然,除 IE 之外的任何浏览器都可以正常工作。
正在发生的问题是,当您在文本区域之外单击时,心爱的 IE 会取消选择任何选定的文本。这导致对象 sel.text 上没有文本。
我像这样运行该函数:
<button onclick="formatString($('#textid')[0],'italic')">Italic</button>
当您单击按钮时,其他浏览器输出
'the italic text' // output the [em]italic[/em] text - when italic is selected
IE output 'the [em][/em]italic text'
如果这与另一个线程相同,请引导我找到它,实际上我已经阅读了 200 多个线程,但没有任何内容指出这个问题。
Here we go again. I've been looking all day for a solution for this problem...
I got the code:
var formatString = function(t,style){
var el = $(t);
var pre;
var start;
var end;
var len;
var replace;
switch(style){
case 'italic':
pre = '[em]';
break;
case 'bold':
pre = '[b]';
break;
case 'paragraph':
pre = '[p]';
break;
}
if(document.selection){
el.focus();
var sel = document.selection.createRange();
// Finally replace the value of the selected text with this new replacement one
sel.text = pre + sel.text + pre.replace(/(\[)/,'[/');
} else {
len = el.val().length;
start = t.selectionStart;
end = t.selectionEnd;
sel = el.val().substring(start, end);
replace = pre + sel + pre.replace(/(\[)/,'[/');
el.val(el.val().substring(0,start) + replace + el.val().substring(end,len));
}
}
What I'm doing is creating a simple wysiwyg just to add some formating to the text that obviously is processed to show the correct html tags. All is working great of course with any browser but IE.
The problem that's happening that the beloved IE deselect any text selected when you click outside the textarea. And this is causing that the object sel.text has not text on it.
I run the function like this:
<button onclick="formatString($('#textid')[0],'italic')">Italic</button>
When you click the button other browsers output
'the italic text' // output the [em]italic[/em] text - when italic is selected
IE output 'the [em][/em]italic text'
If this is the same as another thread just guide me to it, literally I have read over 200 threads and nothing pointed out this problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,因为没有人有答案,所以我扩展了我的研究并阅读了大约 100 个线程,这是一个笑话。
我在以下线程中找到了答案:
为什么从文本区域获取文本选择在单击按钮时有效,但在单击“div”时无效(Internet Explorer)
如何阻止按钮提交表单。
两者都在 stackoverflow.com 中,
无论出于何种原因,当单击按钮以外的元素时,IE 都会失去文本区域的焦点,在我的例子中,跨度看起来像带有 css 的按钮。但问题是,单击按钮提交表单,而我不希望发生这种情况,这就是我选择屏蔽跨度的原因。
由于 IE 行为,我改为使用按钮标记,并在 onclick 属性中添加:
该解决方案的唯一问题如下,如果函数抛出异常,请以任何方式提交表单。所以我必须稍微改变一下代码以避免异常。我的一个坏代码习惯。使代码难以调试。
有关此内容的更多信息,请参阅最后链接的线程。
OK, because nobody has an answer I extended my research and read around 100 threads more, it's a joke.
I found the answer in the following threads:
Why getting a text selection from textarea works when a button clicked but not when a "div" clicked (Internet Explorer)
How to prevent buttons to submit a form.
Both here in stackoverflow.com,
For any reason IE lose the focus out of the textarea when an element other than a button is clicked, in my case a span made to look like a button with css. But the problem is that clicking a button submit a form and I didn't want that to happened, that's why I choose to masked a span.
Because IE behavior I used the button tag instead and in the onclick attribute add:
The only problem with this solution is as follow, if a function throws an exception submit the form any way. So I have to change the code a little bit to avoid exceptions. A bad code habit of mine. Making the code hard to debug.
More info on this in the last thread linked.