2015-12-10 68 views
-1

我寫它打印這是由它通過JavaScript渲染點擊一個按鈕觸發了DOM的某一區域的功能請參見下面的代碼...獲得「未捕獲的SyntaxError:意外的標記}

function MarkQuiz() { 
    var CorrectAnswers = 0, 
     TotalQuestions = 0, 
     CurrentQuestion = "", 
     Percentage = 0, 
     Output = ""; 

    $("select").each(function(key,value) { 
     CurrentQuestion = "#" + $(value).attr("id"); 
     TotalQuestions = key + 1; 

     if($(CurrentQuestion).val() == "Correct") { 
      CorrectAnswers++; 
     } 
    }); 

    Percentage = (CorrectAnswers/TotalQuestions) * 100; 

    Output = "You Scored..." + 
      "<h1>"+Percentage+"%</h1>" + 
      "Which means you got " + CorrectAnswers + " out of " + TotalQuestions + " correct.<br/>" + 
      "<br/><a href='#' onclick='PrintCertificate('#QuizMainContent')' class='button' id='PrintCertificate'>Print your Certificate</a>"; 

    $("#QuizMainContent").html(Output); 
} 

function PrintCertificate(DOMArea) { 
    var PrintArea = $(DOMArea), 
    PrintWindow = window.open('',''); 
    PrintWindow.document.write($(PrintArea).html()); 
    PrintWindow.document.close(); 
    PrintWindow.focus(); 
    PrintWindow.print(); 
    PrintWindow.close(); 

} 

然而,當我點擊該按鈕,我收到此錯誤... enter image description here

這是URL到項目:http://historicalperiods.esy.es/

我在做什麼錯在ADVA 謝謝NCE。

+0

好心分享URL,因爲它不是文件的問題,但帶有URL –

+0

那麼,當你點擊它提供給你的方便鏈接時會出現什麼代碼? –

+0

錯誤消息說錯誤來自不同的文件。點擊控制檯中的鏈接轉到該文件。 – Quentin

回答

1
<a href="#" onclick="PrintCertificate(" #quizmaincontent')'="" class="button" id="PrintCertificate">Print your Certificate</a> 

這是在您的HTML。這是什麼導致了這個問題。正如你所看到的,你"打開屬性,那麼你在你的JavaScript函數中使用",並且可以在功能'關閉該參數,則關閉屬性與',然後你做=""(這沒有意義)。

的正確方法應該是:

<a href="#" onclick="PrintCertificate('#quizmaincontent')" class="button" id="PrintCertificate">Print your Certificate</a> 
+0

我仍然收到此錯誤。這是我所做的... \t \t \t'
Print your Certificate'; –

+0

您仍然打開'onclick =「」'屬性,但也可以在函數中使用參數「'''將雙引號(''')改爲單引號(''')。像這樣:'onclick =「PrintCertificate('#QuizMainContent')」' –

0

感謝您的幫助......

我一點點修改爲L JA提供的答案糾正了這個...

'<br/><a href="#" onclick="PrintCertificate(\'#QuizMainContent\')" class="button" id="PrintCertificate">Print your Certificate</a>'; 
+0

你不**有**來逃避參數。因爲你可以使用'''in''''因爲它看到'''作爲字符串的一部分,所以:'onclick =''「'工作,而'onclick =」「」'不起作用,因爲第一個'「'關閉屬性。 –

相關問題