我在使用http://snippets.dzone.com/posts/show/4973和scrollTop
建议的一些JavaScript来创建一个书签,用于在Blogger的新帖子textarea
中插入一个预设的字符串。代码如下所示:scrollTop输出而不是设置?
//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;
的scrollTop
建议在Firefox中失败,而不是用的值替换在浏览器中当前页面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有点复杂的事情。有任何想法吗?
编辑:除了添加原始页面检测,并提示框替换预设的文字,我能够加入到底解决了原来的问题:
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();
};
如果没有把握最后一个分号是绝对必要或不是,但哦,解决方案!
一个提示:为了开发目的,将脚本url从“http://...whatever.js”更改为“http://...whatever.js?”+ Math.random()... 。这将确保您始终获得最新版本的文件。你会发现这使得书签开发变得更容易,因为你并不总是必须编辑书签本身,只需编辑Web服务器上的文件即可。如果完成后您可以将其压缩为一个独立的书签,如果不是,请使用托管版本。 – rob 2010-11-08 23:04:17
谢谢,抢。直到现在我还没有意识到这种方法。我尝试通过减小书签的长度来测试长度:... javascript:var myField = document.getElementById('postingHtmlBox'); var textAreaScrollPosition = myField.scrollTop; myField.scrollTop = textAreaScrollPosition; ...不幸的是,同样的错误发生。 – CartoonChess 2010-11-08 23:26:00
上面编辑了我的回复。缺乏大括号似乎是问题...我认为这是必要的,如果你在一个小书签中声明变量。 – rob 2010-11-08 23:42:14