我有
一个btn
<button id="next" onclick="inlastSegment()">Next</button>
和一个URL
http://localhost:8888/projects/oop/2
我的目标
当我点击那个 btn 时,我想增加我的 URL 的 lastSegment。
例子
-
http://localhost:8888/projects/oop/3
-
http://localhost:8888/projects/oop/4
-
http://localhost:8888/projects/oop/5
我试过了
<script type="text/javascript">
var href = location.href; // http://localhost:8888/projects/oop/2
var lastSegment = window.location.pathname.split('/').pop();
console.log(lastSegment); //2
function inlastSegment() {
lastSegment = (parseInt(lastSegment)+1);
console.log(lastSegment); //3
//Stuck
}
</script>
我有点卡住了,任何提示/建议将不胜感激。
是否有可能在 PHP 中实现这一点?只是出于好奇。
最佳答案
使用location.replace
如下:
<script type="text/javascript">
var pathArr = window.location.pathname.split('/'), lastSegment = pathArr.pop();
function inlastSegment() {
pathArr.push(++lastSegment);
location.replace(pathArr.join('/'));
}
</script>
关于javascript - 单击按钮时增加当前 URL 的最后一段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31796061/