0

让我们认为我们的 URL 看起来像

www.domain.com #CourseID#LessonID

我想要做的是,当:

  • CourseDivButton如果当前 url 点击
    • 没有主题标签(www.domain.com),然后添加$(this).data("id")为主题标签(www.domain.com #CourseID
    • 仅包含第一个主题标签 - CourseID(www.domain.com #CourseID ) 然后更改它 (www.domain.com #NEWCourseID )
    • 包含 2 个主题标签 -CourseIDLessonID(www.domain.com #CourseID#LessonID ) 然后删除第二个并先更改 (www.domain.com #NEWCourseID )
  • LessonDivButton如果当前 url 点击
    • 仅包含第一个主题标签 - CourseID(www.domain.com #CourseID ) 然后添加第二个 (www.domain.com #CourseID#LessonID )
    • 包含 2 个主题标签 -CourseIDLessonID(www.domain.com #CourseID#LessonID ) 然后更改第二个并保持原样 (www.domain.com #CourseID#NEWLessonID )

我的代码现在如下所示。无法弄清楚如何实现我想要的。

CourseDivButton.click(function(){
    var id=$(this).data("id");
    LoadLessons(id);
    window.location.hash = id;
});

LessonDivButton.live("click", function(){
    var id=$(this).data("id");
    LoadQuestions(id);
    window.location.hash = id;
});

有什么建议么?

4

1 回答 1

1

您不能使用查询字符串而不是使用主题标签吗?我建议你像这样添加它们:

www.domain.com?course= CourseID &lesson= LessonID

使用下面的 javascript 函数,您可以从 URL 中获取变量,如下所示:

var courseID = urlParam('course');
var lessonID = urlParam('lesson');

function urlParam(name) {
    /// <summary>
    /// Gets the value of a parameter located within the url.
    /// </summary>
    /// <param name="name">The paramter to lookup.</param>
    /// <returns type="string">The value of the requested parameter, or undefined if the parameter is not present.</returns>
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return undefined;
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}

如果您需要构建一个添加了查询字符串的新 URL,请查看此内容。

于 2012-09-07T16:50:32.930 回答