Jquery 当给包装增加新高度时,我的屏幕会跳到页面顶部

Jquery 当给包装增加新高度时,我的屏幕会跳到页面顶部,jquery,animation,Jquery,Animation,我有一个带有页眉内容页脚的wwebpage。在内容中我有一个包装器,在这个包装器中我得到了一个扩展自身的表单。当我扩展表单时,我会更改包装的最小高度,以便页脚位于包装的下方。我使用这个jquery代码: $('#formWrapper').animate({'min-height':totalHeight}, { duration: 400, queue: false }); 现在每次我点击一个按钮,它都会用一个动画来扩展这个包装。但是当它变成动画时,我的屏幕就会转到顶部,我自己也看不到动画。

我有一个带有页眉内容页脚的wwebpage。在内容中我有一个包装器,在这个包装器中我得到了一个扩展自身的表单。当我扩展表单时,我会更改包装的最小高度,以便页脚位于包装的下方。我使用这个jquery代码:

$('#formWrapper').animate({'min-height':totalHeight}, { duration: 400, queue: false });
现在每次我点击一个按钮,它都会用一个动画来扩展这个包装。但是当它变成动画时,我的屏幕就会转到顶部,我自己也看不到动画。我怎样才能把焦点放在动画发生的那部分屏幕上

多谢各位

按钮:

<td colspan="3" id="todu2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" id="newItemBoxButton" ><img src="images/addicon.png" /> Voeg een nieuwe regel toe</a></td>

处理程序:

var tdCount =1;
var hiddenCount =1;
$("#newItemBoxButton").click(function(){
            tdCount++;
            hiddenCount++;
            $("#addNewRowAfterThisRow" ).before( "<tr id='itemBox"+tdCount+"'><td valign='top' nowrap='nowrap' class='style1' width='10'>"+tdCount+"</td><td valign='top' ><textarea name='item"+tdCount+"' id='item2"+tdCount+"' rows='4' cols='70' placeholder='Item'></textarea></td><td>&nbsp;</td><td valign='top' class='style3new'><input class='amountBox' type='text' id='amount"+tdCount+"' name='amount"+tdCount+"' value='' placeholder='0' /></td><td  valign='top' class='style3new'> <a href='#' class='deleteItemBoxButton' style='vertical-align:middle' ><img src='images/deleteicon.png'  /></a></td></tr>" );

    //add formheight with 120px < which is the total height of 1 TR
var fieldsetHeight = document.getElementById("fieldset2").offsetHeight;
var totalHeight = fieldsetHeight + 120;
    $('#formWrapper').animate({'min-height':totalHeight}, { duration: 400, queue: false });

    //-----------------------------------------------------------------------------------------------> HIER BEN JE GEBLEVEN DE SCREEN JUMPT STEEDS NAAR  DE TOP: http://stackoverflow.com/questions/32268336/when-adding-new-height-to-wrapper-my-screen-jumps-to-the-top-of-the-page
});
var-tdCount=1;
var-hiddenCount=1;
$(“#newItemBoxButton”)。单击(函数(){
tdCount++;
hiddenCount++;
$(“+tdCount+”)之前的“#addNewRowAfterThisRow”);
//添加带有120px<的formheight,这是1 TR的总高度
var fieldsetHeight=document.getElementById(“fieldset2”).offsetHeight;
var totalHeight=fieldsetHeight+120;
$(“#formWrapper”).animate({'min-height':totalHeight},{duration:400,queue:false});
//----------------------------------------------------------------------------------------------->在屏幕上,您可以看到跳骏马的头像:http://stackoverflow.com/questions/32268336/when-adding-new-height-to-wrapper-my-screen-jumps-to-the-top-of-the-page
});

您有几个选项可以解决此问题

href=“#”
替换为
href=“javascript:void(0);”
#
用于页面内导航,意思是后面跟一个id为
#element id
的元素。因为它是空的,所以它只是引用文档本身,使其跳转到顶部

或者,您可以添加
返回false,或者使用jQuery的
preventDefault()


正如我所预料的那样,这个按钮会将您发送到页面顶部

href=“#”
是空书签,位于页面顶部。这相当于在页面顶部放置书签锚,并在链接中使用其名称

您可以使用
preventDefault
方法轻松停止链接的默认操作:

$("#newItemBoxButton").click(function(e){
  e.preventDefault();
  ...

我怀疑是按钮做的。按钮和处理事件的代码是什么样子的?
$("#newItemBoxButton").click(function(e){
  e.preventDefault();
  ...