【问题标题】:How do I use jQuery to redirect to variable URL?如何使用 jQuery 重定向到可变 URL?
【发布时间】:2015-08-15 14:57:06
【问题描述】:

我正在尝试为我的作品集制作照片库。我碰壁了,不知道为什么这不起作用:

这是一个按钮,可根据您当前的 URL 将您发送到一个 URL。

在本例中,用户位于“/project1.html”,单击下一步按钮,将被定向到“/project2.html”

<button class="next"><img src="images/next.png" class="next-prev-button" alt="Next"></button>

^^^ html 中的按钮

<script>
    var projectnumber = 1;

    $( ".next" ).click(function() {
        projectnumber = projectnumber + 1;
        window.location = "project" + projectnumber + ".html";
    )};
</script>

当我点击按钮时,什么也没有发生。

【问题讨论】:

  • 你的结束脚本错了,改成});

标签: jquery variables url redirect button


【解决方案1】:

你只是有错误的括号序列(在脚本的末尾')}'应该是'})')

$( ".next" ).click(function() {
    projectnumber = projectnumber + 1;
    window.location = "project" + projectnumber + ".html";
});

【讨论】:

  • 德普。非常感谢!
【解决方案2】:

问题是 projectnumber 变量在加载或每个页面时都会被重置,所以它总是 2。

尝试从url获取当前项目号

$(".next").click(function () {
    var projectnumber = +location.pathname.split('/').pop()[0].match(/\d+/)[0] + 1;
    window.location = "project" + projectnumber + ".html";)
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-16
    • 2011-08-24
    • 2014-08-14
    • 2012-02-25
    • 1970-01-01
    • 1970-01-01
    • 2013-02-20
    • 2021-05-18
    相关资源
    最近更新 更多