2010-03-20 33 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