scrollTop 输出而不是设置?

发布于 2024-10-01 05:17:00 字数 3405 浏览 7 评论 0原文

我正在使用 http://snippets.dzone.com/posts/show/4973< 中的一些 JavaScript /a> 及其下方的 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;

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青丝拂面 2024-10-08 05:17:00

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

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

其中 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:

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

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.

慵挽 2024-10-08 05:17:00

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

As per the edited question, adding myField.focus(); to the end solved the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文