为什么我的头衔只要他们应该是3倍?

时间:2013-07-15 04:16:08

标签: javascript jquery ruby-on-rails title

这是我的website

标题适用于About College section

注意:我正在使用Purl

var seg2 = '';
if (url.segment(2) == 'college-life')
    seg2 = "College Life";
else if (url.segment(2) == 'academics')
    seg2 = "Academics";
else if (url.segment(2) == 'living-environment')
    seg2 = "Living Environment";
else if (url.segment(2) == 'social-life')
    seg2 = "Social Life";
var title = "About College";
if (seg2 != '')
    title += " | " + seg2;
if ($("#question_sub").text() != '')
    title += " | " + $("#question_sub").text();
document.title = title;

但不是常规College pages

var seg2 = '';
if (url.segment(2) == 'academics')
    seg2 = "Academics";
else if (url.segment(2) == 'living-environment')
    seg2 = "Living Environment";
else if (url.segment(2) == 'social-life')
    seg2 = "Social Life";
var title = document.title;
if (seg2 != '')
    title += " | " + seg2;
var seg3 = url.segment(3);
seg3[0].toUpperCase();
seg3.slice(1);
title += " | " + seg3;
if ($("#question_sub").text() != '')
    title += " | " + $("#question_sub").text();
document.title = title;

此外,通过搞乱标题,不知何故主页的jquery停止了工作 此外,我试图将seg3资本化似乎并没有起作用。

这是college_pages.js(减去一些重复):

$(document).ready(function(){   
    url = $.url();

    $("#academics_nav, #living_environment_nav, #social_life_nav, .questions, .sections, #major_list, .stratify").hide();
    if (url.segment(1) == 'about-college') {
        $("#advice_nav").show();
        $("#advice").css("font-weight", "bold");
        $("#advice").css("color", "blue");

        if (url.segment(3) == 'is-it-hard-to-do-all-the-things-you-want-to-do')
            $("#question_sub").text('Is it hard to do all the things you want to do? (ex. schoolwork, research, volunteering, clubs, exercise, sports, hobbies, friends, personal time, sleep) Tips/advice on handling it?');
        if (url.segment(3) == 'is-it-easier-to-procrastrinate-in-college')
            $("#question_sub").text('Is it easier to procrastinate in college, being that you often live with your friends, and have more freedom to do what you want? How does this effect most kids? Tips/advice on handling it?');
        if (url.segment(3) == 'what-kinds-of-school-traditions-are-there')
            $("#question_sub").text('What kinds of school traditions are there? Are they fun or stupid?');

        var seg2 = '';
        if (url.segment(2) == 'college-life')
            seg2 = "College Life";
        else if (url.segment(2) == 'academics')
            seg2 = "Academics";
        else if (url.segment(2) == 'living-environment')
            seg2 = "Living Environment";
        else if (url.segment(2) == 'social-life')
            seg2 = "Social Life";
        var title = "About College";
        if (seg2 != '')
            title += " | " + seg2;
        if ($("#question_sub").text() != '')
            title += " | " + $("#question_sub").text();
        document.title = title;

        $("#advice").click(function() {
            $(".questions, .sections, #question_sub, #disqus_thread").hide()
            $(".about_college_nav_item:not(#advice)").css("font-weight", "normal");
            $("#advice").css("font-weight", "bold");
            $(".about_college_nav_item:not(#advice)").css("color", "black");
            $("#advice").css("color", "blue");
            $("#advice_nav").show();
        });
        $("#applying").click(function() {
            $(".questions, .sections, #question_sub, #disqus_thread").hide()
            $(".about_college_nav_item:not(#applying)").css("font-weight", "normal");
            $("#applying").css("font-weight", "bold");
            $(".about_college_nav_item:not(#applying)").css("color", "black");
            $("#applying").css("color", "blue");
            $("#applying_nav").show();
        });
        $("#college_life").click(function() {
            $(".questions, .sections").hide()
            $(".about_college_nav_item:not(#college_life)").css("font-weight", "normal");
            $("#college_life").css("font-weight", "bold");
            $(".about_college_nav_item:not(#college_life)").css("color", "black");
            $("#college_life").css("color", "blue");
            $("#college_life_nav .questions").show();
        });
        if (url.segment(2) == 'college-life') {
            $(".questions, .sections").hide()
            $(".about_college_nav_item:not(#college_life)").css("font-weight", "normal");
            $("#college_life").css("font-weight", "bold");
            $(".about_college_nav_item:not(#college_life)").css("color", "black");
            $("#college_life").css("color", "blue");
            $("#college_life_nav .questions").show();
        }
        $("#academics").click(function() {
            $(".questions, .sections").hide()
            $(".about_college_nav_item:not(#academics)").css("font-weight", "normal");
            $("#academics").css("font-weight", "bold");
            $(".about_college_nav_item:not(#academics)").css("color", "black");
            $("#academics").css("color", "blue");
            $("#about_academics_nav .questions").show();
        });
        if (url.segment(2) == 'academics') {
            $(".questions, .sections").hide()
            $(".about_college_nav_item:not(#academics)").css("font-weight", "normal");
            $("#academics").css("font-weight", "bold");
            $(".about_college_nav_item:not(#academics)").css("color", "black");
            $("#academics").css("color", "blue");
            $("#about_academics_nav .questions").show();
        }
        $("#living_environment").click(function() {
            $(".questions, .sections").hide()
            $(".about_college_nav_item:not(#living_environment)").css("font-weight", "normal");
            $("#living_environment").css("font-weight", "bold");
            $(".about_college_nav_item:not(#living_environment)").css("color", "black");
            $("#living_environment").css("color", "blue");
            $("#about_living_environment_nav .questions").show();
        });
        if (url.segment(2) == 'living-environment') {
            $(".questions, .sections").hide()
            $(".about_college_nav_item:not(#living_environment)").css("font-weight", "normal");
            $("#living_environment").css("font-weight", "bold");
            $(".about_college_nav_item:not(#living_environment)").css("color", "black");
            $("#living_environment").css("color", "blue");
            $("#about_living_environment_nav .questions").show();
        }
        $("#social_life").click(function() {
            $(".questions, .sections").hide()
            $(".about_college_nav_item:not(#social_life)").css("font-weight", "normal");
            $("#social_life").css("font-weight", "bold");
            $(".about_college_nav_item:not(#social_life)").css("color", "black");
            $("#social_life").css("color", "blue");
            $("#about_social_life_nav .questions").show();
        });
        if (url.segment(2) == 'social-life') {
            $(".questions, .sections").hide()
            $(".about_college_nav_item:not(#social_life)").css("font-weight", "normal");
            $("#social_life").css("font-weight", "bold");
            $(".about_college_nav_item:not(#social_life)").css("color", "black");
            $("#social_life").css("color", "blue");
            $("#about_social_life_nav .questions").show();
        }
    }
    else {
        if (url.segment().length < 2) {
            $("#academics_nav").show();
            $("#academics").css("font-weight", "bold");
            $("#academics").css("color", "blue");
            $("#classes").css("font-style", "italic");
            $("#classes").css("color", "#6666FF");
            $(".questions_header").text("Classes");
            $("#classes_nav .questions").show();
        }

        if (url.segment(4) == 'do-professors-explain-things-clearly-are-professors-interesting') {
            $("#question_sub").text('Do they make things easy to understand? Do they make you want to go to class? Do they make you more or less interested in the subject?');
            $(".stratify").show();
        }
        if (url.segment(4) == 'are-professors-fair') {
            $("#question_sub").text('Are they fair? strict? (think tests, assignments, rules...)');
            $(".stratify").show();
        }
        if (url.segment(4) == 'how-hot-are-the-girls-is-it-easy-to-hook-up-if-youre-a-guy')
            $("#question_sub").text('How hot are the girls? Is it easy to hook up if you’re a guy?');
        if (url.segment(4) == 'how-hot-are-the-guys-is-it-easy-to-hook-up-if-youre-a-girl')
            $("#question_sub").text('How hot are the boys? Is it easy to hook up if you’re a girl? (I know... that shouldn’t even be a question).');
        if (url.segment(4) == 'how-often-to-people-smoke-weed')
            $("#question_sub").text('How often do people smoke weed? What about other drugs?');

        $("#academics").click(function() {
            $("#living_environment_nav, #social_life_nav").hide();
            $(".main_college_nav_item:not(#academics)").css("font-weight", "normal");
            $("#academics").css("font-weight", "bold");
            $(".main_college_nav_item:not(#academics)").css("color", "black");
            $("#academics").css("color", "blue");
            $("#academics_nav").show();
            $("#question_sub").css("margin-top", "150px");
        });
        if (url.segment(2) == 'academics') {
            $("#living_environment_nav, #social_life_nav").hide();
            $(".main_college_nav_item:not(#academics)").css("font-weight", "normal");
            $("#academics").css("font-weight", "bold");
            $(".main_college_nav_item:not(#academics)").css("color", "black");
            $("#academics").css("color", "blue");
            $("#academics_nav").show();
            $("#question_sub").css("margin-top", "150px");
        }
            $("#professors").click(function() {
                $(".questions, .sections, #major_list").hide();
                $(".subnav_item:not(#professors)").css("font-style", "normal");
                $("#professors").css("font-style", "italic");
                $(".subnav_item:not(#professors)").css("color", "black");
                $("#professors").css("color", "#6666FF");
                $(".questions_header").text("Professors");
                $("#professors_nav .questions").show();
            });
            if (url.segment(3) == 'professors')
            {
                $(".questions, .sections, #major_list").hide();
                $(".subnav_item:not(#professors)").css("font-style", "normal");
                $("#professors").css("font-style", "italic");
                $(".subnav_item:not(#professors)").css("color", "black");
                $("#professors").css("color", "#6666FF");
                $(".questions_header").text("Professors");
                $("#professors_nav .questions").show();
            }
            $("#classes").click(function() {
                $(".questions, .sections, #major_list").hide();
                $(".subnav_item:not(#classes)").css("font-style", "normal");
                $("#classes").css("font-style", "italic");
                $(".subnav_item:not(#classes)").css("color", "black");
                $("#classes").css("color", "#6666FF");
                $(".questions_header").text("Classes");
                $("#classes_nav .questions").show();
            });
            if (url.segment(3) == 'classes') {
                $(".questions, .sections, #major_list").hide();
                $(".subnav_item:not(#classes)").css("font-style", "normal");
                $("#classes").css("font-style", "italic");
                $(".subnav_item:not(#classes)").css("color", "black");
                $("#classes").css("color", "#6666FF");
                $(".questions_header").text("Classes");
                $("#classes_nav .questions").show();
            }
            $("#difficulty").click(function() {
                $(".questions, .sections, #major_list").hide();
                $(".subnav_item:not(#difficulty)").css("font-style", "normal");
                $("#difficulty").css("font-style", "italic");
                $(".subnav_item:not(#difficulty)").css("color", "black");
                $("#difficulty").css("color", "#6666FF");
                $(".questions_header").text("Difficulty");
                $("#difficulty_nav .questions").show();
            });
            if (url.segment(3) == 'difficulty') {
                $(".questions, .sections, #major_list").hide();
                $(".subnav_item:not(#difficulty)").css("font-style", "normal");
                $("#difficulty").css("font-style", "italic");
                $(".subnav_item:not(#difficulty)").css("color", "black");
                $("#difficulty").css("color", "#6666FF");
                $(".questions_header").text("Difficulty");
                $("#difficulty_nav .questions").show();
            }
            $("#major").click(function() {
                $(".questions, .sections, #major_list").hide();
                $(".subnav_item:not(#major)").css("font-style", "normal");
                $("#major").css("font-style", "italic");
                $(".subnav_item:not(#major)").css("color", "black");
                $("#major").css("color", "#6666FF");
                $("#major_list").show();
                var a = 0;
                original_link = [];
                $(".major_question").each(function() {
                    original_link[a] = $(this).attr("href");
                    a++;
                });
            });
                $("#major_list li").click(function() {
                    $("#major_sub").text($(this).text());
                    $("#major_nav .questions").hide();
                    $("#question_sub, #disqus_thread, .stratify").hide();
                    $("#major_nav .questions").show("fast");
                    major = $(this).text();
                    major = major.toLowerCase();
                    major = major.replace(" ", "-");
                    major = major.replace(" ", "-");
                    major = major.replace(" ", "-");
                    major = major.replace(" ", "-");
                    major = major.replace(" ", "-");
                    major = major.replace(" ", "-");
                    major = major.replace(" ", "-");
                    major = major.replace(" ", "-");
                    major = major.replace(" ", "-");
                    major = major.replace(" ", "-");
                    var i = 0;
                    $(".major_question").each(function() {
                        link = original_link[i];
                        i++;
                        link = "/" + url.segment(1) + "/academics/" + major + "/" + link;
                        $(this).attr("href", link);
                    });
                });
        if (url.segment(4) == 'what-do-you-think-of-the-major-requirements' || 
            url.segment(4) == 'are-you-satisfied-with-the-course-offerings' || 
            url.segment(4) == 'what-was-your-favorite-class' || 
            url.segment(4) == 'what-was-your-least-favorite-class' || 
            url.segment(4) == 'is-there-anything-that-makes-this-program-special' || 
            url.segment(4) == 'what-do-kids-typically-do-with-the-degree' || 
            url.segment(4) == 'do-you-have-any-regrets-with-this-major' || 
            url.segment(4) == 'are-you-well-prepared-for-your-career' || 
            url.segment(4) == 'how-valuable-do-you-think-your-degree-is' || 
            url.segment(4) == 'how-strong-is-the-alumni-network' || 
            url.segment(4) == 'how-were-the-career-services' || 
            url.segment(4) == 'what-are-you-doing-now') {
                $(".questions, .sections, #major_list").hide();
                $(".subnav_item:not(#major)").css("font-style", "normal");
                $("#major").css("font-style", "italic");
                $(".subnav_item:not(#major)").css("color", "black");
                $("#major").css("color", "#6666FF");
                $("#major_list").show();
                var a = 0;
                original_link = [];
                $(".major_question").each(function() {
                    original_link[a] = $(this).attr("href");
                    a++;
                });
                $("#major_nav .questions").hide();
                $("#major_nav .questions").show("fast");
                major = url.segment(3);
                major = major.replace("-", " ");
                major = major.replace("-", " ");
                major = major.replace("-", " ");
                major = major.replace("-", " ");
                major = major.replace("-", " ");
                major = major.replace("-", " ");
                major = major.replace("-", " ");
                major = major.replace("-", " ");
                major = major.replace("-", " ");
                major = major.replace("-", " ");
                $("#major_sub").text(major);
                $("#major_sub").css("text-transform", "capitalize");
                var i = 0;
                $(".major_question").each(function() {
                    link = original_link[i];
                    i++;
                    $(this).attr("href", link);
                });
            }
        $("#social_life").click(function() {
            $("#academics_nav, #living_environment_nav").hide();
            $(".main_college_nav_item:not(#social_life)").css("font-weight", "normal");
            $("#social_life").css("font-weight", "bold");
            $(".main_college_nav_item:not(#social_life)").css("color", "black");
            $("#social_life").css("color", "blue");
            $("#social_life_nav").show();
            $("#question_sub").css("margin-top", "103px");
        });
        if (url.segment(2) == 'social-life')
        {
            $("#academics_nav, #living_environment_nav").hide();
            $(".main_college_nav_item:not(#social_life)").css("font-weight", "normal");
            $("#social_life").css("font-weight", "bold");
            $(".main_college_nav_item:not(#social_life)").css("color", "black");
            $("#social_life").css("color", "blue");
            $("#social_life_nav").show();
            $("#question_sub").css("margin-top", "103px");
        }
            $("#kids").click(function() {
                $(".questions, .sections, #major_list").hide();
                $(".subnav_item:not(#kids)").css("font-style", "normal");
                $("#kids").css("font-style", "italic");
                $(".subnav_item:not(#kids)").css("color", "black");
                $("#kids").css("color", "#6666FF");
                $(".questions_header").text("Kids");
                $("#kids_nav .questions").show();
            });
            if (url.segment(3) == 'kids')
            {
                $(".questions, .sections, #major_list").hide();
                $(".subnav_item:not(#kids)").css("font-style", "normal");
                $("#kids").css("font-style", "italic");
                $(".subnav_item:not(#kids)").css("color", "black");
                $("#kids").css("color", "#6666FF");
                $(".questions_header").text("Kids");
                $("#kids_nav .questions").show();
            }
            $("#parties").click(function() {
                $(".questions, .sections, #major_list").hide();
                $(".subnav_item:not(#parties)").css("font-style", "normal");
                $("#parties").css("font-style", "italic");
                $(".subnav_item:not(#parties)").css("color", "black");
                $("#parties").css("color", "#6666FF");
                $(".questions_header").text("Parties");
                $("#parties_nav .questions").show();
            });
            if (url.segment(3) == 'parties')
            {
                $(".questions, .sections, #major_list").hide();
                $(".subnav_item:not(#parties)").css("font-style", "normal");
                $("#parties").css("font-style", "italic");
                $(".subnav_item:not(#parties)").css("color", "black");
                $("#parties").css("color", "#6666FF");
                $(".questions_header").text("Parties");
                $("#parties_nav .questions").show();
            }
        $("#living_environment").click(function() {
            $("#academics_nav, #social_life_nav").hide();
            $(".main_college_nav_item:not(#living_environment)").css("font-weight", "normal");
            $("#living_environment").css("font-weight", "bold");
            $(".main_college_nav_item:not(#living_environment)").css("color", "black");
            $("#living_environment").css("color", "blue");
            $("#living_environment_nav").show();
            $("#question_sub").css("margin-top", "150px");
            $("#question_sub, #disqus_thread, .stratify").hide();
        });
        if (url.segment(2) == 'living-environment')
        {
            $("#academics_nav, #social_life_nav").hide();
            $(".main_college_nav_item:not(#living_environment)").css("font-weight", "normal");
            $("#living_environment").css("font-weight", "bold");
            $(".main_college_nav_item:not(#living_environment)").css("color", "black");
            $("#living_environment").css("color", "blue");
            $("#living_environment_nav").show();
            $("#question_sub").css("margin-top", "150px");
        }
            $("#campus").click(function() {
                $(".questions, .sections, #major_list").hide();
                $(".subnav_item:not(#campus)").css("font-style", "normal");
                $("#campus").css("font-style", "italic");
                $(".subnav_item:not(#campus)").css("color", "black");
                $("#campus").css("color", "#6666FF");
                $(".questions_header").text("Campus");
                $("#campus_section, #campus_section .questions").show();
            });
            if (url.segment(3) == 'campus') {
                $(".questions, .sections, #major_list").hide();
                $(".subnav_item:not(#campus)").css("font-style", "normal");
                $("#campus").css("font-style", "italic");
                $(".subnav_item:not(#campus)").css("color", "black");
                $("#campus").css("color", "#6666FF");
                $(".questions_header").text("Campus");
                $("#campus_section, #campus_section .questions").show();
            }
            $("#food").click(function() {
                $(".questions, .sections, #major_list").hide();
                $(".subnav_item:not(#food)").css("font-style", "normal");
                $("#food").css("font-style", "italic");
                $(".subnav_item:not(#food)").css("color", "black");
                $("#food").css("color", "#6666FF");
                $(".questions_header").text("Food");
                $("#food_nav, #food_nav .questions").show();
            });
            if (url.segment(3) == 'food') {
                $(".questions, .sections, #major_list").hide();
                $(".subnav_item:not(#food)").css("font-style", "normal");
                $("#food").css("font-style", "italic");
                $(".subnav_item:not(#food)").css("color", "black");
                $("#food").css("color", "#6666FF");
                $(".questions_header").text("Food");
                $("#food_nav, #food_nav .questions").show();
            }
            $("#housing").click(function() {
                $(".questions, .sections, #major_list").hide();
                $(".subnav_item:not(#housing)").css("font-style", "normal");
                $("#housing").css("font-style", "italic");
                $(".subnav_item:not(#housing)").css("color", "black");
                $("#housing").css("color", "#6666FF");
                $(".questions_header").text("Housing");
                $("#housing_nav").show();
            });
            if (url.segment(3) == 'housing') {
                $(".questions, .sections, #major_list").hide();
                $(".subnav_item:not(#housing)").css("font-style", "normal");
                $("#housing").css("font-style", "italic");
                $(".subnav_item:not(#housing)").css("color", "black");
                $("#housing").css("color", "#6666FF");
                $(".questions_header").text("Housing");
                $("#housing_nav").show();
            }
            $("#weather").click(function() {
                $(".questions, .sections, #major_list").hide();
                $(".subnav_item:not(#weather)").css("font-style", "normal");
                $("#weather").css("font-style", "italic");
                $(".subnav_item:not(#weather)").css("color", "black");
                $("#weather").css("color", "#6666FF");
                $(".questions_header").text("Weather");
                $("#weather_section").show();
            });
            if (url.segment(3) == 'weather') {
                $(".questions, .sections, #major_list").hide();
                $(".subnav_item:not(#weather)").css("font-style", "normal");
                $("#weather").css("font-style", "italic");
                $(".subnav_item:not(#weather)").css("color", "black");
                $("#weather").css("color", "#6666FF");
                $(".questions_header").text("Weather");
                $("#weather_section").show();
            }
            $("#safety").click(function() {
                $(".questions, .sections, #major_list").hide();
                $(".subnav_item:not(#safety)").css("font-style", "normal");
                $("#safety").css("font-style", "italic");
                $(".subnav_item:not(#safety)").css("color", "black");
                $("#safety").css("color", "#6666FF");
                $(".questions_header").text("Safety");
                $("#safety_nav .questions").show();
            });
            if (url.segment(3) == 'safety') {
                $(".questions, .sections, #major_list").hide();
                $(".subnav_item:not(#safety)").css("font-style", "normal");
                $("#safety").css("font-style", "italic");
                $(".subnav_item:not(#safety)").css("color", "black");
                $("#safety").css("color", "#6666FF");
                $(".questions_header").text("Safety");
                $("#safety_nav .questions").show();
            }
        $("#information").click(function() {
            $("#academics_nav, #living_environment_nav, #social_life_nav, .questions, .sections, #major_list, #question_sub, #disqus_thread, .stratify").hide();
            $(".main_college_nav_item:not(#information)").css("font-weight", "normal");
            $("#information").css("font-weight", "bold");
            $(".main_college_nav_item:not(#information)").css("color", "black");
            $("#information").css("color", "blue");
            $(".subnav_item").css("font-style", "normal");
            $(".subnav_item").css("color", "black");
            $("#information_section").show();
        });
        $("#other").click(function() {
            $("#academics_nav, #living_environment_nav, #social_life_nav, .questions, .sections, #major_list, #question_sub, #disqus_thread, .stratify").hide();
            $(".main_college_nav_item:not(#other)").css("font-weight", "normal");
            $("#other").css("font-weight", "bold");
            $(".main_college_nav_item:not(#other)").css("color", "black");
            $("#other").css("color", "blue");
            $("#other_section").show();
        });

        var seg2 = '';
        if (url.segment(2) == 'academics')
            seg2 = "Academics";
        else if (url.segment(2) == 'living-environment')
            seg2 = "Living Environment";
        else if (url.segment(2) == 'social-life')
            seg2 = "Social Life";
        var title = document.title;
        if (seg2 != '')
            title += " | " + seg2;
        var seg3 = url.segment(3);
        seg3[0].toUpperCase();
        seg3.slice(1);
        title += " | " + seg3;
        if ($("#question_sub").text() != '')
            title += " | " + $("#question_sub").text();
        document.title = title;
    }
});

2 个答案:

答案 0 :(得分:0)

替换:

seg3[0].toUpperCase();
seg3.slice(1);

使用:

seg3 = seg3[0].toUpperCase() + seg3.substr(1);

Javascript字符串是不可变对象,您无法在适当的位置修改它们。字符串函数返回带有结果的新字符串,您必须将它们分配回变量。

答案 1 :(得分:0)

我刚刚将var title = document.title;更改为var title = $('#college_name').text();。问题是javascript文件被多次加载(我不知道为什么)。 http://blog.develop.com/blogs/bid/232857/Preventing-JavaScript-Files-from-Loading-Multiple-Times#.UeT3gGRhn_E可能也会解决我的问题,但我选择了更简单的解决方案。