jQuery - 重定向到页面后更改页面的属性

时间:2014-09-18 21:05:36

标签: javascript jquery html wordpress

所以我正在制作一个wordpress网站,因此,我希望将自定义的javascript保持在最低限度。 基本上,我有Page1和Page2,每个都有几个子页面。在Page1-children中,有指向Page2-children的链接。 Page2-children中有一个“后退”按钮,当前只是导航到Page2。我想要完成的是:如果Page2-child被导航到一个Page1-child,我想将“Back”按钮的href设置为它导航的页面。这是我到目前为止所得到的:

jQuery(document).ready(function($){

    $('.application-button').on('click', function(event){
        event.preventDefault();
        var currURL = window.location;
        window.location.href = event.currentTarget.href;
        $('.dynamic-link').attr('href', currURL); //<-- .dynamic-link is a class of the Back button
        //I figured that if it came after the window.location change, it would still be called

    });

});

如果代码之前的解释难以理解,我很抱歉,但我无法更详细地解释它,因为它对某些工作非常敏感。

感谢任何和所有帮助!

1 个答案:

答案 0 :(得分:1)

一种好方法是导航到javascript中的最后一页

<script>
function goBack() {
    window.history.back()
}
</script>

<body>
<button onclick="goBack()">Go Back</button>
</body>

http://www.w3schools.com/jsref/met_his_back.asp

在你的情况下:

$('.application-button').on('click', function(event){window.history.back();});