javascript button handler, error !
-
28-10-2019 - |
문제
could anyone please tell me wats wrong with :
<a href="javascript:void();" onclick="myFunc()"> Execute function </a>
in chrome inspector the error i get is :
Uncaught SyntaxError: Unexpected token <
below is how my code looks like :
<!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>
해결책
<a href="javascript:void();" onclick="myFunc()"> Execute function </a>
<script>
function myFunc() {
// some code here
}
</script>
Put the anchor tag out of script tag, you are putting tags inside javascript so that is why error is result.
제휴하지 않습니다 StackOverflow