3

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

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

4

3 回答 3

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>';
于 2013-04-25T00:41:38.317 回答
2

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

前任:

    <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>
于 2013-04-25T03:48:14.070 回答
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>'
...

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

于 2013-04-25T00:36:59.690 回答