0

您好,我想每天显示一个特定的文本,但我不知道以下代码中的问题出在哪里,我使用的是 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\”&lt;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 -->
4

2 回答 2

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>");
于 2013-09-15T17:49:31.727 回答
0

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

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

我已经\在这个演示小提琴中编辑并添加了

于 2013-09-15T17:26:09.210 回答