2011-03-23 36 views
0

任何人都可以告訴我WATS錯:javascript按鈕處理程序,錯誤!

<a href="javascript:void();" onclick="myFunc()"> Execute function </a> 

在Chrome檢查我的錯誤是:

未捕獲的SyntaxError:意外的標記<

下面

如何我的代碼看起來像:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
</head> 


    <body> 


     <div id="root"></div> 
     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script> 

     <a href="javascript:void();" onclick="myFunc()"> Execute function </a> 
     <script> 


     function myFunc() { 
      // some code here 
    }  
     </script> 
    </body> 
</html> 

回答

0
<a href="javascript:void();" onclick="myFunc()"> Execute function </a> 

<script> 
function myFunc() { 
    // some code here 
}  
</script> 

將錨標記放在腳本標記之外,您將標記放在JavaScript中,這就是爲什麼錯誤是結果。

+0

把你的腳本放在頭部和身體裏面的標籤..多數民衆贊成在良好的做法 – 2011-03-23 07:27:36