manipulador de botão javascript, erro!
-
28-10-2019 - |
Pergunta
Alguém poderia me dizer o que há de errado com:
<a href="javascript:void();" onclick="myFunc()"> Execute function </a>
no inspetor do cromo, o erro que recebo é:
Não capturado SyntaxError: token inesperado <
abaixo está a aparência do meu código:
<!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>
Solução
<a href="javascript:void();" onclick="myFunc()"> Execute function </a>
<script>
function myFunc() {
// some code here
}
</script>
Coloque a tag âncora fora da tag do script, você está colocando tags dentro do javascript, então é por isso que o erro é o resultado.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow