gpt4 book ai didi

javascript - 主干 pushState 路由

转载 作者:行者123 更新时间:2023-11-29 10:43:57 25 4
gpt4 key购买 nike

我正在尝试处理 backbone.js pushState 以更改漂亮网址上的主题标签。使用 localhost 并且我的脚本路径是 http://localhost/test/backbone/test.html。但每次点击都将我带到 localhost/login。我做错了什么?

var AppRouter = Backbone.Router.extend({
routes: {
"login": "getPost",
"*actions": "defaultRoute"
}
});

var app_router = new AppRouter;

app_router.on('route:getPost', function (id) {
alert( "login" );
});

app_router.on('route:defaultRoute', function (actions) {
alert( actions );
});

app_router.navigate("/login", {trigger: true});
Backbone.history.start({pushState: true, root: '/login/'});

最佳答案

你需要添加:

$(document).delegate("a", "click", function(evt) {
var href = $(this).attr("href");
var protocol = this.protocol + "//";

if (href.slice(protocol.length) !== protocol && protocol !== 'javascript://' && href.substring(0, 1) !== '#') {
evt.preventDefault();

Backbone.history.navigate(href, true);
}
});

最后两个字符串应该是:

app_router.navigate("/login", {trigger: true}); // <- should remove this string
Backbone.history.start({pushState: true, root:"test/backbone/test.html"});

关于javascript - 主干 pushState 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23218417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com