未捕获的SyntaxError:意外的保留字

时间:2013-04-25 00:34:45

标签: javascript jquery

我在JQUERY中有这段代码:

var large='<div class="vip" style ="background:url(./uploads/deal/"+data[a].id+"_1.jpg);">
           <span class ="coundtown" value ='+data[a].deal_time_off+' style ="display:none;"></span>
          <script> $(document).ready(function(){ $('+data[a].deal_unique_id+').countdown({ date:'+data[a].deal_time_off+'}); }); </script>
       <div class = "deal_info clearfix">
           <div class = "deal_subject clearfix">
               <a href="./deal/'+data[a].deal_unique_id+'">adsfd</a>
           </div>
           <div style = "clear:both"></div>
           <div class="timer">
             <img class="img" src="./img/stopwatch.png" border="none" />
             <span class ='+data[a].deal_unique_id+'></span>
           </div>
         </div>
         <div class="percent_mini"></div>
     </div>';
        //$("#left_row").append(large);

但是就这一行:

 <span class ="coundtown" value ='+data[a].deal_time_off+' style ="display:none;">

我收到此错误:

  

未捕获的SyntaxError:意外的保留字

问题是什么?

var large='<div class="vip" style ="background:url(http://mydeal.ge/uploads/deal/"+data[a].id+"_1.jpg);">
           <span class ="coundtown" value ="'+data[a].deal_time_off+'" style ="display:none;"></span>
          <script> $(document).ready(function(){ $("'+data[a].deal_unique_id+'").countdown({ date:"'+data[a].deal_time_off+'"}); }); </script>
       <div class = "deal_info clearfix">
           <div class = "deal_subject clearfix">
               <a href="http://mydeal.ge/deal/'+data[a].deal_unique_id+'">adsfd</a>
           </div>
           <div style = "clear:both"></div>
           <div class="timer">
             <img class="img" src="http://mydeal.ge/img/stopwatch.png" border="none" />
             <span class ="'+data[a].deal_unique_id+'"></span>
           </div>
         </div>
         <div class="percent_mini"></div>
     </div>';

我修复了这个变量,但仍然出现错误

3 个答案:

答案 0 :(得分:3)

如果没有转义它们,您就无法在javascript字符串中添加换行符:

var str1 = "abcd
            efgh";

应该是:

var str1 = "abcd\
            efgh";

您的HTML分布在多行,因此您需要转义换行符:

var large='<div class="vip" style ="background:url(./uploads/deal/"+data[a].id+"_1.jpg);">\
       <span class ="coundtown" value ='+data[a].deal_time_off+' style ="display:none;"></span>\
      <script> $(document).ready(function(){ $('+data[a].deal_unique_id+').countdown({\ date:'+data[a].deal_time_off+'}); }); </script>\
   <div class = "deal_info clearfix">\
       <div class = "deal_subject clearfix">\
           <a href="./deal/'+data[a].deal_unique_id+'">adsfd</a>\
       </div>\
       <div style = "clear:both"></div>\
       <div class="timer">\
         <img class="img" src="./img/stopwatch.png" border="none" />\
         <span class ='+data[a].deal_unique_id+'></span>\
       </div>\
     </div>\
     <div class="percent_mini"></div>\
 </div>';

答案 1 :(得分:2)

不要在变量中使用html,更不要在包含html的脚本中使用html,因为它最难维护。你应该使用模板(Handlebarsundersscore,小胡子......)。祝你好运!

前:

    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

      <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.0.0-rc.3/handlebars.min.js"></script>

      <title>your title</title>
    </head>
    <body>
     <script id="our-tpl" type="text/x-handlebars-template">
      <div class="vip" style ="background:url(./uploads/deal{{deal_unique_id}}_1.jpg);"> 
        <span class ="coundtown" value="{{deal_time_off}}" style ="display:none;"></span>
        <div class = "deal_info clearfix">
          <div class = "deal_subject clearfix">
            <a href="./deal/{{deal_unique_id}}">adsfd</a>
          </div>
          <div style = "clear:both"></div>
          <div class="timer">
            <img class="img" src="./img/stopwatch.png" border="none" />
            <span class ="{{deal_unique_id}}"></span>
          </div>
        </div>
        <div class="percent_mini"></div>
      </div>
    </script>
    <div class="render"></div>


    <script type="text/javascript">
      var obj = {deal_unique_id: 1, deal_time_off: "This is your count"},
          tpl =  Handlebars.compile(($('#our-tpl').html())),
          $rend = $('.render');
          //render
          $rend.html(tpl(obj));
      $('.vip').ready(function(){ 
        var el = '.'+obj.deal_unique_id;
        $(el).countdown({ date:obj.deal_time_off}); 
      }); 
    </script>

    </body>
    </html>

答案 2 :(得分:1)

我想你可能会错过value属性值的引号。

<span class ="coundtown" value ="'+data[a].deal_time_off+'" style ="display:none;">

请注意value="...

编辑 - 等待等一下。

除非该字符串全部在一行中,否则您需要将其拆分。

var large='<div class="vip" style ="background:url(./uploads/deal/"+data[a].id+"_1.jpg);"';
    large+='       <span class ="coundtown" value ='+data[a].deal_time_off+' style="display:none;"></span>'
...

你不能在许多不同的行上有一个字符串。