El evento onclick en línea devuelve un "Error de sintaxis no detectado:Token inesperado (” error

StackOverflow https://stackoverflow.com//questions/25091621

  •  02-01-2020
  •  | 
  •  

Pregunta

Tengo un botón que funciona fuera de esto, pero realmente quiero replicar las acciones con solo un div normal.Puedo hacerlo simplemente agregando este controlador de clics:

 editor.insertText("\"≥\"");

a un evento onclick.Quiero agregar ese fragmento a un div en línea pero sigo recibiendo un "Uncaught SyntaxError: Unexpected token ("error del siguiente div:

<div style="background-color:red; width:50px; height: 50px;" onclick='function () { editor.insertText(\\≥\\); }'></div>

......¿Qué estoy haciendo mal?

¿Fue útil?

Solución

Prueba esto:

<div style="background-color:red; width:50px; height: 50px;" onclick='editor.insertText("\"≥\"");'></div>

Otros consejos

Debe invocar la función, si incluye la función directamente en el atributo onclick, debe usar una función inmediata como esta, aunque a continuación debe definir qué editor es primero.

<div style="background-color:red; width:50px; height: 50px;" onclick='(function () { editor.insertText("\\≥\\"); }());'></div>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top