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 做一些有点复杂的事情。有什么想法吗?
编辑:除了添加原始页面检测和用提示框替换预设文本之外,我还可以通过添加 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();
};
不确定最后一个分号是否是绝对必要的,但是哦,好吧,解决方案!
I'm using some JavaScript from http://snippets.dzone.com/posts/show/4973, and the scrollTop
suggestion below it, to create a bookmarklet for inserting a preset string of text into Blogger's new post textarea
. The code looks like this:
//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;
}
}
And the suggestion below that:
//add this to the start of function
textAreaScrollPosition = myField.scrollTop;
//add this to end of the function
myField.scrollTop = textAreaScrollPosition;
The scrollTop
suggestion fails in Firefox, instead replacing the current page in the browser with the value of textAreaScrollPosition
.
I added this to the front of the sandwiched down version for the bookmarklet:
javascript:var myField=document.getElementById('postingHtmlBox');var myValue='lol';
Altogether it reads:
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;
Without line breaks, though.
I am less than a JS wizard. I'm just trying to help a non-tech friend do something a little complex with Blogger. Any ideas?
EDIT: In addition to adding primitive page detection and replacing the preset text with a prompt box, I was able to solve the original problem by adding myField.focus();
to the end:
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();
};
Not sure if that last semicolon is strictly necessary or not but oh well, solutions!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的书签可能太长,无法放入书签。一个选项是使用动态脚本标签:
其中 myscript.js 是执行该工作的实际脚本。
如果您将其保留为独立的书签,请务必用花括号将整个内容(在“javascript:”之后)括起来。
You may be making your bookmarklet too long to fit in a bookmark. An option is to use a dynamic script tag:
Where myscript.js is the actual script that does the work.
If you keep it as a self-contained bookmarklet, be sure to surround the whole thing (after "javascript:") with curly braces.
根据编辑的问题,在末尾添加
myField.focus();
解决了问题。As per the edited question, adding
myField.focus();
to the end solved the issue.