0

URLが次のようになっているとしましょう

www.domain.com #コースID#レッスンID

私がやろうとしているのは、次の場合です。

  • CourseDivButton現在の URL の場合にクリック
    • ハッシュタグがない場合 (www.domain.com)、$(this).data("id")ハッシュタグとして追加するだけです (www.domain.com #CourseID )
    • 最初のハッシュタグのみを含む - CourseID(www.domain.com #CourseID ) その後、変更します (www.domain.com #NEWCourseID )
    • 2 つのハッシュタグが含まれています -CourseIDそしてLessonID(www.domain.com #CourseID#LessonID ) 次に 2 番目を削除し、最初に変更します (www.domain.com #NEWCourseID )
  • LessonDivButton現在の URL の場合にクリック
    • 最初のハッシュタグのみを含む - CourseID(www.domain.com #CourseID ) 次に 2 番目の (www.domain.com #CourseID#LessonID )を追加
    • 2 つのハッシュタグが含まれます -CourseIDそしてLessonID(www.domain.com #CourseID#LessonID ) 2 番目を変更し、1 番目はそのまま (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 に答える