我的switch语句或document.write();不工作,我无法解决它

时间:2013-09-15 17:22:24

标签: javascript jquery mobile

您好我想每天显示一个特定的文本,我不知道下面的代码中的问题在哪里,我使用jQuery mobile,我认为javascript是可以在网页上执行此操作的。

<div data-role="content">
            <div>
            <script>
                var n = new Date().getDay();
                var ch = new String();



                switch(n)
                {
                    case 0:
                    {
                    ch = String("\“It is not so easy to leave your comfort zone, it is a very difficult thing to do, 
                    but it most certainly will change your future and make you a better person than you ever could imagine\”<br><br>Today\'s challenge:<br><br>
                    Do you live in the future?<br><br>
                    Become aware of moments when you are lost in thinking about the future, whether it is 10 minutes or 2 years from now. 
                    Notice how it pulls your attention from the present moment. See the reasons why you are running from your current experience.<br>");

                    }
                    break;

                    case 1:
                    ch = String("A dream is your creative vision for your life in the future. You must break out of your current comfort zone and become comfortable with the unfamiliar and the unknown.-Denis Waitley<br><br>
                    Today\'s challenge(Do one of them, or more if you like so...):<br><br>
                    Do everyday things differently. 
                    Take a different route to work. 
                    Try a new restaurant without checking Yelp first. Go vegetarian for a week, or a month. 
                    Try a new operating system. Recalibrate your reality. 
                    Whether the change you make is large or small, make a change in the way you do things on a day-to-day basis. 
                    Look for the perspective that comes from any change, even if it\'s negative. 
                    Don\'t be put off if things don\'t work out the way you planned.");
                    break;

                    case 2:
                    ch = String("Life begins at the end of your comfort zone. -Neale Donald Walsch<br<br>
                    Today\'s challenge:<br><br>
                    ");
                    break;

                    case 3:
                    ch = String("The comfort zone is the great enemy to creativity; moving beyond it necessitates intuition, 
                    which in turn configures new perspectives and conquers fears.- Dan Stevens<br><br>
                    Today\'s challenge:<br><br>
                    Trust yourself and make snap decisions. We\'re contradicting ourselves, but there\'s a good reason. 
                    Just as there are people who thrive on snap decisions, others are more comfortable weighing all of 
                    the possible options several times, over and over again. Sometimes making a snap call is in order, 
                    just to get things moving. Doing so can help you kickstart your personal projects and teach you to trust your judgement. 
                    It\'ll also show you there\'s fallout to quick decisions as well as slow ones.");
                    break;

                    case 4:
                    ch = String("Comedians tend to find a comfort zone and stay there and do lamer versions of themselves for the rest of their career.- Chris Rock<br><br>
                    Today\'s challenge:<br><br>
                    Take your time making decisions. 
                    Sometimes slowing down is all it takes to make you uncomfortable—especially if speed and 
                    quick thinking are prized in your work or personal life. Slow down, 
                    observe what\'s going on, take your time to interpret what you see, and then intervene. 
                    Sometimes just defending your right to make an educated decision can push you out of your comfort zone. 
                    Think, don\'t just react.");
                    break;

                    case 5:
                    ch = String("I think when you get people who are really talented and you take them out of their comfort zone, you get a lot more out of them.- Gore Verbinski<br><br>
                    Today\'s challenge:<br><br>
                    Do it in small steps. It takes a lot of courage to break out of your comfort zone. 
                    You get the same benefits whether you go in with both feet as you do if you start slow, so don\'t be afraid to start slow. 
                    If you\'re socially anxious, don\'t assume you have to muster the courage to ask your crush on a date right away, 
                    just say hello to them and see where you can go from there. Identify your fears, and then face them step by step.");
                    break;

                    case 6:
                    ch = String("When you go out of your comfort zone and it works there's nothing more satisfying.- Kristen Wiig<br><br>
                    Today\'s challenge:<br><br>
                    Be aware of attachment to objects<br><br>
                    Notice your attachments to objects such as a cool sweater, a new gadget or any other thing that would make you feel bad if you lost it. 
                    What is the deep reason for being attached to them? Can you find the strength to let go of the attachment and give the object away?");
                    break;
                }

                document.write("<p>" + ch.big() + "</p>");
            </script>
            </div>
            <form>
                <label for="textarea-1">Post on Facebook:</label>
                <textarea cols="40" rows="8" name="textarea-1" id="textarea-1" >
                </textarea>
            </form>

            <input type="submit" value="Post to Facebook">
        </div><!-- /content -->

2 个答案:

答案 0 :(得分:0)

Javascript将每个新行视为单独的语句,并且您正在使用多行字符串

所以在字符串(..)中的每一行使用\使其成为一个字符串

我已在此演示fiddle

中编辑并添加了\

答案 1 :(得分:0)

有几件事。就像影子说的那样,你需要确保正确处理多线字符串。同样在case 0:中,您不需要主条款周围的花括号。你有正确的其他情况。

我将提出一个可能会让它变得更容易的建议。将所需信息保存在对象中的数组中:

obj = {

  0: [
    'day one',
    'this is what\'s happened',
    'we\'re separating up the text so that we keep the line lengths manageable'
  ],

  1: [
    'blah blah',
    'blah blah'
    ]

};

然后你所要做的就是:

var n = new Date().getDay();
var info = obj[n].join('');
document.write("<p>" + info.big() + "</p>");