【问题标题】:Uncaught SyntaxError: Unexpected token ILLEGAL when putting html in var未捕获的 SyntaxError:将 html 放入 var 时出现意外令牌非法
【发布时间】:2012-10-24 08:56:30
【问题描述】:

我只是将一些 html 放入 var 中,但出现此错误: Uncaught SyntaxError: Unexpected token ILLEGAL

typeForm = "Type de RDV : <br />                                                                                                                                                                                   
            <input type='radio' name='rdv-type' value='0' class='rdv-type' />Client seul<br />                                                                                                                     
            <input type='radio' name='rdv-type' value='1' class='rdv-type' />Client + con<br />                                                                                                           
            <input type='radio' name='rdv-type' value='2' class='rdv-type' />Visite agence<br />                                                                                                                   
            <input type='radio' name='rdv-type' value='4' class='rdv-type' />Signature PV<br />                                                                                                                    
            <input type='radio' name='rdv-type' value='5' class='rdv-type' />Con step<br />                                                                                                           
            <input type='radio' name='rdv-type' value='3' class='rdv-type' />Land analysis<br />";

我不明白我做错了什么。我首先收到警告&lt;br /&gt;

我查看了其他帖子,但我真的不明白为什么会这样。

【问题讨论】:

标签: javascript


【解决方案1】:

这是因为在 JavaScript 中你不能简单地以字符串值开始新行,你应该'转义新行'

var smth = "Some text \
            continues here \
            and here";

或使用字符串连接:

var smth = "Some text " +
           "continues here " +
           "and here";

【讨论】:

  • 啊,非常感谢。我对此一无所知。我是 jQuery/js 的新手,您先生教了我一些我一个人永远无法理解的东西。
【解决方案2】:
typeForm = "Type de RDV : <br/>" +                                                                                                                                                                                   
        "<input type='radio' name='rdv-type' value='0' class='rdv-type' />Client seul<br />" +                   
....

等等

【讨论】:

    【解决方案3】:

    当换行时,在每个新行之前添加一个“+”

    【讨论】:

    • 谢谢。我将 VisioN 解决方案与 \ 一起使用,但这也很好用,谢谢 :)
    【解决方案4】:

    您的 html 字符串格式不正确,将所有行连接成一个包含多行的字符串。

    Live Demo

    typeForm = "Type de RDV : <br /> " +                                                                                                                                                                                  
               " <input type='radio' name='rdv-type' value='0' class='rdv-type' />Client seul<br />      " +                                                                                                               
               " <input type='radio' name='rdv-type' value='1' class='rdv-type' />Client + con<br />      " +                                                                                                     
              "  <input type='radio' name='rdv-type' value='2' class='rdv-type' />Visite agence<br />       " +                                                                                                            
               " <input type='radio' name='rdv-type' value='4' class='rdv-type' />Signature PV<br />         " +                                                                                                           
                "<input type='radio' name='rdv-type' value='5' class='rdv-type' />Con step<br />           " +                                                                                                
        " <input type='radio' name='rdv-type' value='3' class='rdv-type' />Land analysis<br />";
    
        alert(typeForm );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-09
      • 2017-04-06
      • 1970-01-01
      • 1970-01-01
      • 2016-01-28
      • 2018-03-11
      相关资源
      最近更新 更多