2017-05-05 70 views
-2

我已經在我的JavaScript文件下面的代碼:如何解決JavaScript SyntaxError:無效或意外的令牌錯誤?

input = '<input id="ytRequirementsForm_additional_f__12" type="hidden" value="" name="RequirementsForm[additional][f__12][]" /> 
    <input class="form-control" name="RequirementsForm[additional][f__12][]" id="RequirementsForm_additional_f__12" type="file" /> 
<div class="RequirementsForm_additional_f__12 fileprogress" style="display: none"><div class="filename"> 
       <span class="glyphicon glyphicon-paperclip" aria-hidden="true"></span> 
       <strong></strong> 
       <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button> 
      </div> 
      <div class="progress"> 
       <div class="progress-bar progress-bar-striped active" role="progressbar" aria-valuenow="1" 
        aria-valuemin="0" aria-valuemax="100"> 
        <span class="percent">0</span>% 
         </div> 
      </div> 
      <div class="info_danger_text"><p></p></div> 
     </div> 
     '; 

但是,當我跑我的web應用程序,它顯示未捕獲SyntaxError: Invalid or unexpected token錯誤。我該如何解決它?

+0

貌似缺少'「'在失去了一些東西,也許'字符串 – Eruant

+0

年底」'在結束了嗎? – guradio

+2

你不能這樣做JS中的多行字符串。要麼使用模板字符串要麼跳過換行符。 – Sirko

回答

2

我看到您的「字符串」中有換行符。在JavaScript中,如果要將字符串分配給包含換行符的變量,則應在行末尾使用「\」。 以下是已更正的代碼。

input = '<input id="ytRequirementsForm_additional_f__12" type="hidden" value="" name="RequirementsForm[additional][f__12][]" />\ 
<input class="form-control" name="RequirementsForm[additional][f__12][]" id="RequirementsForm_additional_f__12" type="file" />\ 
<div class="RequirementsForm_additional_f__12 fileprogress" style="display: none">' 

我希望它能幫助你:)

2

只能在ES6中使用多行字符串。要做到這一點,你必須使用反引號而不是引號。

MDN example

0

你應該只有一行聲明字符串變量。如果你想將它寫在多行,你可以通過做:

  1. 聲明它拼接每個符合+

    input = '<input id="ytRequirementsForm_additional_f__12" type="hidden" value="" name="RequirementsForm[additional][f__12][]" />' + 
     
         '<input class="form-control" name="RequirementsForm[additional][f__12][]" id="RequirementsForm_additional_f__12" type="file" />' + 
     
        '<div class="RequirementsForm_additional_f__12 fileprogress" style="display: none">';

  2. 使用模板文字功能https://css-tricks.com/template-literals/

input = `<input id="ytRequirementsForm_additional_f__12" type="hidden" value="" name="RequirementsForm[additional][f__12][]" /> 
 
      <input class="form-control" name="RequirementsForm[additional][f__12][]" id="RequirementsForm_additional_f__12" type="file" /> 
 
     <div class="RequirementsForm_additional_f__12 fileprogress" style="display: none">`;

相關問題