2017-05-10 6 views
0

Meine html finden:JavaScript kippt Funktion in Onclick Attribute

<!doctype hmtl> 
 
<html> 
 
<head> 
 
</head> 
 
<body> 
 
    <script type='text/javascript'> 
 
     var playerId = 1; 
 
     function event(action) { 
 
      let req = new XMLHttpRequest('/act?id='+String(playerId)+'&action='+action); 
 
      alert(action); 
 
     } 
 
    </script> 
 
    <img src="up.png" width='100' height='100' onclick='event("up");'> 
 
    <img src="down.png" width='100' height='100' onclick='event("down");'> 
 
    <img src="left.png" width='100' height='100' onclick='event("left");'> 
 
    <img src="right.png" width='100' height='100' onclick='event("right");'> 
 
    <img src="fire.png" width='100' height='100' onclick='event("fire");'> 
 
</body> 
 
</html>

ich sicher weiß das Bild anklickbar ist. Aber warum sagt die Fehlermeldung event is not a function? Ich brauche Hilfe.

+3

einfach die Funktion umbenennen; "Event" ist nicht erlaubt. –

+0

Danke! Das ist die Antwort. –

Antwort

2

Auf diese Weise versuchen Sie auf das globale Objekt window.event zuzugreifen, benennen Sie Ihre Funktion einfach um.

Ihre eigenen Code, um die Funktion Umbenennen event-test:

<!doctype hmtl> 
 
<html> 
 
<head> 
 
</head> 
 
<body> 
 
    <script type='text/javascript'> 
 
     var playerId = 1; 
 
     function test(action) { 
 
      let req = new XMLHttpRequest('/act?id='+String(playerId)+'&action='+action); 
 
      alert(action); 
 
     } 
 
    </script> 
 
    <img src="up.png" width='100' height='100' onclick='test("up");'> 
 
    <img src="down.png" width='100' height='100' onclick='test("down");'> 
 
    <img src="left.png" width='100' height='100' onclick='test("left");'> 
 
    <img src="right.png" width='100' height='100' onclick='test("right");'> 
 
    <img src="fire.png" width='100' height='100' onclick='test("fire");'> 
 
</body> 
 
</html>