簡體   English   中英

如何從導航欄href鏈接調用servlet,以及如何調用jquery函數以使單擊時緩慢向下滾動

[英]How to call a servlet from a nav bar href link , and also call a jquery function for slow scroll down on click

如何從導航欄href鏈接調用servlet,同時調用jquery函數以緩慢向下滾動

我試圖從onclick href鏈接調用servlet,它正在調用但未同時調用jquery函數

   Code for slow scroll down

    $("#apply").click(function() {
        $('html,body').animate({
            scrollTop : $("#applyleave").offset().top
        }, 500);
    });

 Code for calling servlet
function myFunction1() {
        window.location.href = "http://localhost:9091/LeaveManagementProject/ApproveLeave";
    }

沒有錯誤信息,只是在調用一個函數,而不是在enter code hereenter code here

如果要獲取數據並在獲取數據期間滾動,則需要停止請求並使用XHR進行處理。

// this makes an asynchronous request to fetch data and executes the callback function
var sendXHRPost = void 0;
{
    sendXHRPost = function (url, params, callback) {
        var http = new XMLHttpRequest();
        http.open('POST', url, true);
        http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
        http.onreadystatechange = function() {
            if (http.readyState === 4) {
                callback(http.status, http.responseText, clickedElement);
            }
        }
        http.send(params);
    }
}
// your scroll function
var scrollFunction = void 0;
{
    scrollFunction = function (status, response, clickedElement) {
        // update your DOM with reponse here
        // Then scroll
        $("#apply").click(function() {
            $('html,body').animate({
                scrollTop : $(clickedElement.href).offset().top
            }, 500);
        });
    }
}
// Handle the click on your element
var clickFunction = void 0;
{
    clickFunction = function (e) {
        e.preventDefault();
        var myURL = "http://localhost:9091/LeaveManagementProject/ApproveLeave";
        sendXHRPost(myURL, null, scrollFunction, e.currentTarget);
    }
}
document.getElementById("apply1").addEventListener("click", clickFunction, false);

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM