javascript 未捕获的语法错误:意外的保留字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16204532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 03:41:58  来源:igfitidea点击:

Uncaught SyntaxError: Unexpected reserved word

javascriptjquery

提问by Gia Nebieridze

I have this code in 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);

But on this line:

但在这一行:

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

I get this error:

我收到此错误:

Uncaught SyntaxError: Unexpected reserved word

未捕获的语法错误:意外的保留字

what is the problem?

问题是什么?

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>';

i fixed this variable but still getting error

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

回答by Dennis

You can't have newlines in javascript strings without escaping them:

javascript 字符串中不能有换行符而不转义它们:

var str1 = "abcd
            efgh";

should be:

应该:

var str1 = "abcd\
            efgh";

Your HTML is spread out over multiple lines so you need to escape the line breaks:

您的 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>';

回答by Gustavo S. Rodrigues

Don't use html in a variable, much less a script containing html, because it's most difficult to maintenance. do you should use template (Handlebars, undersscore, mustache, ...). good luck!

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

Ex:

前任:

    <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>

回答by hvgotcodes

I think maybe you are missing quotes around the value for the valueattribute.

我想也许您缺少围绕value属性值的引号。

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

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

note the value="...

注意 value="...

EDIT -- wait wait wait a minute.

编辑 - 等等等等。

Unless that string is all in one line, you need to split it up.

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

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>'
...

you can't have one string across many different lines.

您不能在许多不同的行中使用一个字符串。