2010-03-20 59 views
0

我工作的一个Web应用程序,它模拟的页面中,现在被发现在:our serverjQuery scrollTo插件滚动屏幕到div不会工作,为什么?

如果你点击蓝色的标题“第一步”,然后选择“交付,以解决”,表单的选项将显示使用jQuery ajax加载。这没有问题。点击“场地”单选按钮会带你到另一个表格,对此也没有问题。

如果你稍微向下滚动,你可以看到一个textarea,最重要的是,你可以看到一个名为链接“这是什么?”。点击它,textarea应填入示例文字。

问题是,点击链接后,网页自动滚动到顶部。我想要的是在点击链接后将textarea保持在屏幕的中心位置。

我试图使用jQuery插件叫做“scrollTo”,可以发现here

从它的演示页,我可以告诉正是我想要的。这里是我的代码尝试使用它:

function reloadDeliveryForm() 
{ 
$('#deliveryForm').load('./ajax/deliveryToVenueForm.html', 
      function(response,  status, xhr) 
{ 
    if (status == "error") 
    { 
     $.prompt("Sorry but there was an error, please try again.<br />" + 
       "If same error persists, please report to webmaster."); 
    } 
    else //deliveryForm loaded successfully 
    { 
     validateDeliveryForm(); 
     $("#delivery_special").elastic(); 
     $('#special_conditions_tip').click(function() 
     { 
      console.log("start filling"); 
      fillTextareaWithExample(); 
      console.log("end filling"); 
      $.scrollTo('#delivery_special'); 
      console.log("end scrolling"); 
     }); 
    } 
}); 

}

从Firebug的输出,我可以告诉scrollTo函数被调用,但不起作用。我已将jQuery切换回版本1.3.2,该版本用于插件的演示页面,但这也无济于事。

我的编码有问题吗?您将使用哪种技术来解决此问题?

任何建议,非常感谢。

回答

1

约。您custom.js线112,改变这种

$('#special_conditions_tip').click(function() 
{ 
    console.log("start filling"); 
    fillTextareaWithExample(); 
    console.log("end filling"); 
    $.scrollTo('#delivery_special'); 
    console.log("end scrolling"); 
}); 

这个

$('#special_conditions_tip').click(function(event) 
{ 
    event.preventDefault(); 
    console.log("start filling"); 
    fillTextareaWithExample(); 
    console.log("end filling"); 
    $.scrollTo('#delivery_special'); 
    console.log("end scrolling"); 
}); 

.preventDefault()防止浏览器行为的发生。

+0

感谢您的信息!我一直在尝试其他方法来解决这个问题,并发现如果我添加“return false”到这个代码片段的末尾,那么这仍然会做。虽然我认为jQuery的preventDefault()是两者中较好的一个。 – 2010-03-20 06:31:29