我有这样的代码:
<a href="javascript://" onclick="$('#newMsgTxt').focus();">New message</a>
<div style="min-height: 1300px;"></div>
<textarea id="newMsgTxt"></textarea>
<div style="min-height: 500px;"></div>
问题来了: 单击链接后,在不同的浏览器中页面会滚动到不同的位置。
点击后 textarea
在屏幕上的位置示例:
Chrome:居中
FF:底部
歌剧:顶级
如何让所有浏览器都像 Chrome 一样工作?
最佳答案
$('a').click(function(e){
e.preventDefault();
var o = $('#newMsgTxt').focus().offset().top, $w = $(window);
$w.scrollTop(o - ($w.height() / 2));
});
关于javascript - jQuery focus() 页面滚动长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14772369/