简体   繁体   English

匿名函数错误:SyntaxError:意外的输入结束

[英]Anonymous function error : SyntaxError: Unexpected end of input

I'm dynamically creating DOM elements in my Javascript code, and I am trying to add an anonymous function as an onclick event handler.我正在我的 Javascript 代码中动态创建 DOM 元素,并且我正在尝试添加一个匿名函数作为onclick事件处理程序。 Here is my code :这是我的代码:

var onClickFunction = "(function(){var x = document.getElementById('onPrsClickButton'); x.value = this.id; x.click();})()";

var libelle = '<div onclick=' + onClickFunction + '></div>'

But whenever I click this <div> , the console gives me this error :但是每当我点击这个<div> ,控制台都会给我这个错误:

SyntaxError: Unexpected end of input

Does anyone know why ?有谁知道为什么? Thanks !谢谢 !

The printed output will be打印输出将是

<div onclick=(function(){var x = document.getElementById('onPrsClickButton'); x.value = this.id; x.click();})()></div>

This is not a valid HTML property value.这不是有效的 HTML 属性值。
To make it valid you must surround the value in quotation marks (") or apostrophes (').要使其有效,您必须将值括在引号 (") 或撇号 (') 中。
Some browsers can withstand some level of abuse, like not writing proper syntax, but that can't be trusted.一些浏览器可以承受一定程度的滥用,比如没有编写正确的语法,但这是不可信的。

Also, your code have a IIFE (Immediately Invoked Function Expression).此外,您的代码具有IIFE (立即调用的函数表达式)。 This means that the code inside the function will be executed right away, not when you click (although I'm not sure the actual behavior in this particular case).这意味着函数内的代码将立即执行,而不是在您单击时执行(尽管我不确定在这种特殊情况下的实际行为)。

To make your sample work, you can do something like this:为了使您的示例工作,您可以执行以下操作:

var onClickFunction = function(){
    // Your event handler
};

var libelle = '<div onclick="javascript:onClickFunction();"></div>'

But the best approach (IMO) to solve your problem is to add a listener to your click, like this:但是解决您的问题的最佳方法 (IMO) 是为您的点击添加一个侦听器,如下所示:

var libelle = document.getElementById("libelle");
libelle.addEventListener('click', function(){
    // Your event handler
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的 SyntaxError:JSON.parse 处的 JSON 输入意外结束(<anonymous> ) - Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) 未捕获的语法错误:JSON.parse 处的 JSON 输入意外结束(<anonymous> )</anonymous> - Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse(<anonymous>) SyntaxError:JSON.parse 处的 JSON 输入意外结束(<anonymous> )</anonymous> - SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) 语法错误:JSON.parse 处的 JSON 输入意外结束(<anonymous> ) on(&quot;数据&quot;) - SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) on("data") 未捕获的语法错误:JSON 输入的意外结束:在 JSON.parse(<anonymous> )</anonymous> - Uncaught SyntaxError: Unexpected end of JSON input : at JSON.parse (<anonymous>) Uncaught SyntaxError:函数中的输入意外结束 - Uncaught SyntaxError: Unexpected end of input in function 错误“未捕获的语法错误:意外的输入结束” - Error "Uncaught SyntaxError: Unexpected end of input" Uncaught SyntaxError:输入错误意外结束 - Uncaught SyntaxError: Unexpected end of input error 未捕获到的SyntaxError:输入意外结束-Javascript错误 - Uncaught SyntaxError: Unexpected end of input - Javascript error 错误:未捕获的语法错误:输入意外结束 - error: Uncaught SyntaxError: Unexpected end of input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM