0

我有这样的代码:

<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: 底部
Opera: 顶部

如何让所有浏览器都像 Chrome 一样工作?

4

5 回答 5

1
$('a').click(function(e){
    e.preventDefault();
    var o = $('#newMsgTxt').focus().offset().top, $w = $(window);
    $w.scrollTop(o - ($w.height() / 2));
});

http://jsfiddle.net/2BHRw/

于 2013-02-08T12:27:08.980 回答
0

试试这个。

onclick="$('#newMsgTxt').focus().val($('#newMsgTxt').val());
于 2013-02-08T12:24:59.500 回答
0

网页:

<a href="" id="newMessage">New message</a>

查询:

$(document).ready(function() {
    $('#newMessage').click(function(e) {
        e.preventDefault();
        $('#newMsgTxt').focus();
    });    
});

http://jsfiddle.net/soyuka/jxZw2/

于 2013-02-08T12:25:16.287 回答
0

这是因为滚动位置不同,

您可以使用console.log(window.scrollY)onclicking 进行测试,link 因此您必须为此编写代码以使其居中对齐。

代码就像

$(function() {
    $('a').click(function(e){
        $('#newMsgTxt').focus();
        var offsetTop = offset().top; 
        $(window).scrollTop(offsetTop - ($(window).height() / 2));
    });
});

你的html就像:

<a href="javascript://">New message</a>
<div style="min-height: 1300px;"></div>
<textarea id="newMsgTxt">gfg</textarea>
<div style="min-height: 500px;"></div>
于 2013-02-08T12:39:54.440 回答
0

嗨试试下面的代码......我的命名不好......所以,根据你的想法替换。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="Scripts/jquery-1.7.1.js" type="text/javascript"></script>
   <script>

       function test() {
           var o = $('#newMsgTxt').focus().offset().top, $w = $(window);
                $w.scrollTop(o - ($w.height() / 2));
       }
   </script>
</head>
<body>
<a  onclick="test();">New message</a>
<div style="min-height: 1300px;vertical-align:top;"></div>
<textarea id="newMsgTxt" ></textarea>
<div style="min-height: 500px;"></div>
</body>
</html>
于 2013-02-08T12:52:58.890 回答