gestore del pulsante javascript, errore!
-
28-10-2019 - |
Domanda
qualcuno potrebbe dirmi qualcosa che non va con:
<a href="javascript:void();" onclick="myFunc()"> Execute function </a>
in Chrome Inspector l'errore che ottengo è:
Errore di sintassi non rilevato: token imprevisto <
sotto è come appare il mio codice:
<!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>
Soluzione
<a href="javascript:void();" onclick="myFunc()"> Execute function </a>
<script>
function myFunc() {
// some code here
}
</script>
Metti l'anchor tag fuori dal tag script, stai inserendo tag all'interno di javascript, ecco perché si verifica un errore.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow