scrollTop输出而不是设置?

时间:2010-11-08 22:43:16

标签: javascript firefox textarea bookmarklet scrolltop

我正在使用http://snippets.dzone.com/posts/show/4973中的一些JavaScript,以及其下方的scrollTop建议,以创建用于将预设文本字符串插入Blogger的新帖子textarea的书签。代码如下所示:

//IE support
if (document.selection) {
    myField.focus();

    //in effect we are creating a text range with zero
    //length at the cursor location and replacing it
    //with myValue
    sel = document.selection.createRange();
    sel.text = myValue;

//Mozilla/Firefox/Netscape 7+ support
} else if (myField.selectionStart || myField.selectionStart == '0') {

    myField.focus();
    //Here we get the start and end points of the
    //selection. Then we create substrings up to the
    //start of the selection and from the end point
    //of the selection to the end of the field value.
    //Then we concatenate the first substring, myValue,
    //and the second substring to get the new value.
    var startPos = myField.selectionStart;
    var endPos = myField.selectionEnd;
    myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
    myField.setSelectionRange(endPos+myValue.length, endPos+myValue.length);
} else {
    myField.value += myValue;
}

}

以下建议:

//add this to the start of function
textAreaScrollPosition = myField.scrollTop;

//add this to end of the function
myField.scrollTop = textAreaScrollPosition;

Firefox中的scrollTop建议失败,而是使用textAreaScrollPosition的值替换浏览器中的当前页面。

我将它添加到书签的夹心版本的前面:

javascript:var myField=document.getElementById('postingHtmlBox');var myValue='lol';

总而言之:

javascript:var myField=document.getElementById('postingHtmlBox');
var myValue='lol';
var textAreaScrollPosition=myField.scrollTop;
if(document.selection){myField.focus();
sel=document.selection.createRange();
sel.text=myValue;
}else if(myField.selectionStart||myField.selectionStart=='0'){myField.focus();
var startPos=myField.selectionStart;
var endPos=myField.selectionEnd;
myField.value=myField.value.substring(0,startPos)+myValue+myField.value.substring(endPos,myField.value.length);
myField.setSelectionRange(endPos+myValue.length,endPos+myValue.length);
}else{myField.value+=myValue;
}myField.scrollTop=textAreaScrollPosition;

但没有换行符。

我不是一个JS向导。我只是想帮助一位非技术朋友用Blogger做一些复杂的事情。有什么想法吗?

编辑:除了添加原始页面检测并用提示框替换预设文本之外,我还可以通过添加myField.focus();来解决原始问题:

javascript:if(document.getElementById('postingHtmlBox')){var myField=document.getElementById('postingHtmlBox');
var myValue=prompt('Insert text here.');
var textAreaScrollPosition=myField.scrollTop;
if(document.selection){myField.focus();
sel=document.selection.createRange();
sel.text=myValue;
}else if(myField.selectionStart||myField.selectionStart=='0'){myField.focus();
var startPos=myField.selectionStart;
var endPos=myField.selectionEnd;
myField.value=myField.value.substring(0,startPos)+myValue+myField.value.substring(endPos,myField.value.length);
myField.setSelectionRange(endPos+myValue.length,endPos+myValue.length);
}else{myField.value+=myValue;
}myField.scrollTop=textAreaScrollPosition;
myField.focus();
};

不确定最后一个分号是否是绝对必要的,但是哦,解决方案!

2 个答案:

答案 0 :(得分:0)

您可能使书签太长而无法放入书签中。一个选项是使用动态脚本标记:

javascript:document.body.appendChild(document.createElement('script')).setAttribute('src','http://mysite/myscript.js')

其中myscript.js是执行工作的实际脚本。

如果你把它作为一个独立的书签,请务必用大括号围绕整个事物(在“javascript:”之后)。

答案 1 :(得分:0)

根据编辑过的问题,在最后添加myField.focus();解决了问题。