大家好,小弟最近在写一个UBB文本编辑器,现在碰到个问题就是,比如我现在已经输入了很多内容了,滚动条在很下面,然后这个时候我点击一下比如 'B' 按钮,这时应该是在最后面加入一个 的标签,可是现在的问题就是,我点击后,滚动条就会自动还原到最上面去,我还得再拖下来继续输入.我试了这么一段代码,在IE下可用,但是在火狐下没效果:
var txt = document.getElementById("txt");
txt.scrollTop=txt.scrollHeight;
不知道谁能帮忙解决下?火狐下应该怎么样才能让滚动条永远停在最底部?
var txt = document.getElementById("txt");
txt.scrollTop=txt.scrollHeight;
不知道谁能帮忙解决下?火狐下应该怎么样才能让滚动条永远停在最底部?
解决方案 »
- 如何把这个html中的js代码分离成.js文件
- 表单禁止提交后,再允许提交,无法进入后台代码的问题
- 为什么 我点击用flash做的按钮的时候 地址显示http:/// 不是完整的地址
- 在Firefox中怎样实现滤镜效果?
- 如何在imageButton中弹出thickbox?
- 如何替换转义字符
- 用xmlhttp发post时已经把ie浏览器的安全设置为"中-低"为什么还有安全提示,怎么设置取消?
- 框架刷新问题(急~~~~~~~~~在线等)
- 求助!vue+element前端开发
- js计算两个日期相差的天数达到30天转换成自然周达到1年转换自然月
- select onchange 问题
- JS美化select下拉菜单后,滚动条不见了
txt.style.position=absolute;
txt.style.top=txt.scrollHeight;
<script>
var txt = document.getElementById("txt");
txt.style.position=absolute;
txt.style.top=txt.scrollHeight;
</script>
只能在IE下用.如果写在textarea的onload里是不是就好了???该怎么写啊!谢谢啦!
window.onload=function(){
var txt = document.getElementById("txt");
txt.style.position=absolute;
txt.style.top=txt.scrollHeight;
};
function addTag(tag){
//crappy browser sniffer
var isFF = false;
var textselected = false;
var startTag,endTag,allTag;
var color,size,img,link;
if(navigator.userAgent.toLowerCase().indexOf("firefox") > 0){
isFF = true;
}
var myArea = document.getElementById("txt");
var begin,selection,end;
if (isFF == true){
if (myArea.selectionStart!= undefined) {
begin = myArea.value.substr(0, myArea.selectionStart);
selection = (myArea.value).substr(myArea.selectionStart, myArea.selectionEnd - myArea.selectionStart);
end = myArea.value.substr(myArea.selectionEnd);
if (selection.length > 0){
textselected = true;
}
}
}else{
if (window.getSelection){
selection = window.getSelection();
}else if (document.getSelection){
selection = document.getSelection();
}else if (document.selection){
selection = document.selection.createRange().text;
}
var startPos = myArea.value.indexOf(selection);
if (startPos!= 0){
var endPos = myArea.value.indexOf(selection) + selection.length;
begin = myArea.value.substr(0,startPos);
end = myArea.value.substr(endPos, myArea.value.length);
textselected = true;
}
} switch(tag)
{
case 'bold':
startTag='[bold]';
endTag='[/bold]';
breakcase 'italic':
startTag='[italic]';
endTag='[/italic]';
breakcase 'underline':
startTag='[underline]';
endTag='[/underline]';
breakcase 'del':
startTag='[delete]';
endTag='[/delete]';
breakcase 'color':
startTag='[color='+ color +']';
endTag='[/color]';
breakcase 'size':
startTag='[size='+ size +']';
endTag='[/size]';
breakcase 'left':
startTag='[align=left]';
endTag='[/align]';
breakcase 'center':
startTag='[align=center]';
endTag='[/align]';
breakcase 'right':
startTag='[align=right]';
endTag='[/align]';
break
}
allTag = startTag + endTag;if(textselected == true){
myArea.value = begin+startTag+selection+endTag+end;
}else{if (document.selection) {
myArea.focus();
sel = document.selection.createRange();
sel.text = allTag;
}
//MOZILLA/NETSCAPE support
else if (myArea.selectionStart || myArea.selectionStart == '0') {
var startPos = myArea.selectionStart;
var endPos = myArea.selectionEnd;
myArea.value = myArea.value.substring(0, startPos)
+ allTag
+ myArea.value.substring(endPos, myArea.value.length);
} else {
myArea.value += allTag;
}}
}这是我加入UBB标签的函数<a href="#" onclick="addTag('bold'); return false;">B</a> 这是加入B标签的按钮
<textarea name="content" id="txt" class="txt"></textarea>这是我的文本域..到底怎么才能自动拉到底部呀?
很明显是href的问题,直接改成
<a href="javascript:void(0)" onclick="addTag('bold'); return false;">B </a>
就可以了