如何使用 jQuery 重定向到变量 URL?
How do I use jQuery to redirect to variable URL?
我正在尝试为我的作品集制作一个照片库。我碰壁了,想不通为什么这行不通:
根据您当前的 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>
当我点击按钮时,没有任何反应。
问题是 projectnumber 变量在加载时或每页都被重置,因此它始终为 2。
尝试从url
中获取当前项目编号
$(".next").click(function () {
var projectnumber = +location.pathname.split('/').pop()[0].match(/\d+/)[0] + 1;
window.location = "project" + projectnumber + ".html";)
});
你只是括号顺序错误(在脚本末尾')}'应该是'})')
$( ".next" ).click(function() {
projectnumber = projectnumber + 1;
window.location = "project" + projectnumber + ".html";
});
我正在尝试为我的作品集制作一个照片库。我碰壁了,想不通为什么这行不通:
根据您当前的 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>
当我点击按钮时,没有任何反应。
问题是 projectnumber 变量在加载时或每页都被重置,因此它始终为 2。
尝试从url
中获取当前项目编号$(".next").click(function () {
var projectnumber = +location.pathname.split('/').pop()[0].match(/\d+/)[0] + 1;
window.location = "project" + projectnumber + ".html";)
});
你只是括号顺序错误(在脚本末尾')}'应该是'})')
$( ".next" ).click(function() {
projectnumber = projectnumber + 1;
window.location = "project" + projectnumber + ".html";
});