简体   繁体   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

How to call a servlet from nav bar href link and also same time call a jquery function for slow scroll down 如何从导航栏href链接调用servlet,同时调用jquery函数以缓慢向下滚动

I tried to call the servlet from onclick href link, it is calling but not calling jquery function at the same time 我试图从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";
    }

No error message, just it is calling only one function not the both enter code here 没有错误信息,只是在调用一个函数,而不是在enter code hereenter code here

If you want to fetch data and scroll during the data is fetched, you need to stop the request and do it with XHR. 如果要获取数据并在获取数据期间滚动,则需要停止请求并使用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);

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM