【问题标题】:scrollTop Outputting Rather Than Setting?scrollTop 输出而不是设置?
【发布时间】:2010-11-08 22:43:15
【问题描述】:

我正在使用来自 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;

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();
};

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

【问题讨论】:

    标签: javascript firefox textarea bookmarklet scrolltop


    【解决方案1】:

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

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

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

    如果您将其保留为独立的小书签,请确保将整个内容(在“javascript:”之后)用大括号括起来。

    【讨论】:

    • 一个提示:出于开发目的,将脚本 url 从“http://...whatever.js”更改为“http://...whatever.js?”+Math.random () ....因为这将确保您始终获得最新版本的文件。您会发现这使书签开发变得更加容易,因为您不必总是编辑书签本身,只需编辑 Web 服务器上的文件。如果,当你完成后,你可以把东西压缩成一个独立的书签,很酷,如果没有,坚持使用托管版本。
    • 谢谢,抢。直到现在我才知道这种方法。我尝试通过将小书签减少到以下来测试长度:... javascript:var myField=document.getElementById('postingHtmlBox');var textAreaScrollPosition=myField.scrollTop;myField.scrollTop=textAreaScrollPosition; ...不幸的是,发生了同样的错误。
    • 编辑了我上面的回复。缺少花括号似乎是问题所在……我认为如果您在书签中声明变量,这是必要的。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-10
      • 2012-12-17
      • 2022-10-04
      • 1970-01-01
      • 1970-01-01
      • 2014-10-24
      • 2015-02-18
      相关资源
      最近更新 更多