简体   繁体   English

页面重新加载后单击按钮

[英]Click on button after page reload

I have some links on the page and when user click on link, page is reloaded. 我在页面上有一些链接,当用户单击链接时,页面会重新加载。

All links have some ID and all have the one same class. 所有链接都有一些ID,并且所有链接都有一个相同的类。

I need to click on some other link after page is reloaded, but only if user click on some of the link with particular class (not anywhere, like menu link). 重新加载页面后,我需要单击其他链接,但前提是用户单击具有特定类的某些链接(而不是任何地方,如菜单链接)。

How I can do something like this using JS or jQuery? 我如何使用JS或jQuery做类似的事情?

It sounds like you are attempting to build a single page application, ie the application is mostly run by JavaScript instead of server side rendering. 听起来您正在尝试构建单个页面应用程序,即该应用程序主要由JavaScript运行而不是服务器端渲染。 In this case page reloads are you enemy. 在这种情况下,页面重装是您的敌人。 A page reload is basically a restart of your page, and while you can keep state using cookies or localStorage , this will provide you with a rather poor user experience. 页面重新加载基本上是页面的重新启动,尽管您可以使用cookie或localStorage保持状态,但这将为您提供较差的用户体验。

In this case, you would just add event handlers to any actions your user would take, and simply handle them in javascript without reloading the page from the server. 在这种情况下,您只需将事件处理程序添加到用户将要执行的任何操作中,并且只需使用javascript处理它们即可,而无需从服务器重新加载页面。 If you need to get more data in response to the user's actions, you can use AJAX to pull that data from the server. 如果您需要获取更多数据来响应用户的操作,则可以使用AJAX从服务器中提取该数据。

If you have to use links with urls (still unclear to me why that would be necessary), I would recommend using hash urls like so '#some-action' which will not reload the page when clicked. 如果您必须使用带有URL的链接(我仍然不清楚为什么这是必要的),我建议您使用哈希URL,例如'#some-action' ,单击该链接将不会重新加载页面。 You can then attach an event handler to the link, or even listen for the url hash to change using the hashchange event. 然后,您可以将事件处理程序附加到链接,甚至可以使用hashchange事件来监听要更改的url哈希。 In this way you can know what link the user pressed. 这样,您可以知道用户按下了什么链接。

If you are not trying to build a single page application, you will need to add code on the server to store the information you need in the page. 如果不尝试构建单个页面应用程序,则需要在服务器上添加代码以在页面中存储所需的信息。

I did it. 我做的。

So, I created function: 因此,我创建了函数:

/**
 * Click on delivery link after page reload
 *
 * @return {boolean} [loacalStorage is available]
 */
$.fn.openDelivery = function(){
    if (localStorage) {
        var addedToCart = localStorage["addCart"];
        if (addedToCart) {
            setTimeout(function() {
                $('#showRight').trigger('click');
            }, 1500);
            localStorage.removeItem("addCart");
        }
        $(this).click(function(e) {
            localStorage["addCart"] = true;
        });

        return true;
    }

    return false;
};

And then called that function: 然后调用该函数:

$(document).ready(function () {
    // Trigger delivery click
    $('.class-of-my-link').openDelivery();
});

In my case, page reloads after user add some item in shopping cart. 在我的情况下,页面会在用户在购物车中添加一些商品后重新加载。 With this function I click on link (with 1.5s delay) that opens cart of the user that is shown on the right side of the screen. 使用此功能,我单击链接(具有1.5秒的延迟),该链接将打开屏幕右侧显示的用户的购物车。

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

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