本文介绍了IE8插入文本到textarea问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些代码将标签插入到textarea(对于Internet Explorer)。
但我有问题与IE8。如果有很多文本,我会尽量在最后插入文本 - 它会滚动。
代码:
< script type =text / javascript>
函数bold()
{
var text1 = document.getElementById('text1');
var sel ='';
if(document.selection)
{
sel = document.selection.createRange();
sel = sel.text;
}
if(sel)
{
text1.focus();
document.selection.createRange()。text ='< strong>'+ sel +'< / strong>';
}
}
< / script>
< br />
< input type =buttonvalue =boldonclick =bold(); />
只有当我将宽度设置为textarea时,才会发生这种情况,所以代码对此标记起作用:
< textarea id =text1rows =10cols =80>< / textarea>
解决方案
两个选项:
- 操纵 scrollTop 属性将滚动条移动到最后:
text1.scrollTop = text1.scrollHeight;
- 使用方法。 li>
I have some code to insert tags to textarea (for Internet Explorer).But I have problem with IE8. If there is lots of text and I try to insert text somewhere in the end - it's scrolled up.
Code:
<script type="text/javascript">
function bold()
{
var text1 = document.getElementById('text1');
var sel = '';
if (document.selection)
{
sel = document.selection.createRange();
sel = sel.text;
}
if(sel)
{
text1.focus();
document.selection.createRange().text = '<strong>' + sel + '</strong>';
}
}
</script>
<textarea id="text1" rows="10" style="width:100%;"></textarea>
<br />
<input type="button" value="bold" onclick="bold();" />
It's happens only if I set width to textarea, so code works ok with this markup:
<textarea id="text1" rows="10" cols="80"></textarea>
解决方案
Two options:
- Manipulate the scrollTop property to move the scrollbar to the end:
text1.scrollTop = text1.scrollHeight;
- Move the caret to the desired position using the moveStart() method.
这篇关于IE8插入文本到textarea问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!