简体   繁体   English

JavaScript错误中的匿名函数

[英]anonymous function in javascript error

I am trying to run a very basic code : 我正在尝试运行一个非常基本的代码:

<a href="#" onClick="function(event){
   event.preventDefault();
   document.getElementById('LogOut-form').submit();
 }">LogOut</a>

I'm not even pretty sure , if this is right or not but when i click on the link, i do see an error. 我什至不是很确定,如果这是正确的,但是当我单击链接时,我确实看到了错误。

Uncaught SyntaxError: Unexpected token ( 未捕获到的SyntaxError:意外令牌(

I just want to submit the form using something like this, is there something that i am missing? 我只想使用这样的方式提交表单,是否缺少某些内容? If,yes, what would be the solution? 如果可以,那将是什么解决方案?

As you are declaring function with onClick use IIF expression 当您使用onClick声明函数时,请使用IIF表达式

 <a href="#" onClick="(
                 function(event){
                    event.preventDefault();  
                    document.getElementById('LogOut-form').submit();
                 })(event) 
          ">LogOut</a>

<a href="#" onClick="(function(event){event.preventDefault();document.getElementById('LogOut-form').submit();})(event)">LogOut</a>

or don't use function expression 或不使用函数表达式

<a href="#" onClick="event.preventDefault(); document.getElementById('LogOut-form').submit();">LogOut</a>

However I would recommend you to get rid of ugly inline click handler and don't use inline click handler in old and bad practices. 但是,我建议您摆脱难看的内联单击处理程序,并且不要在老旧的坏习惯中使用内联单击处理程序。 You should bind event using addEventListener 您应该使用addEventListener绑定事件

HTML 的HTML

<a href="#" id='logout'>LogOut</a>

Script 脚本

document.querySelector('#logout').addEventListener('click', function(event){
    event.preventDefault();
    document.getElementById('LogOut-form').submit();
}, false);

You should not use the on attributes of html elements anymore. 您不应再使用html元素的on属性。 Instead use addEventListener like in the answer of Satpal 而是像在Satpal的答案中那样使用addEventListener

The onclick is evaluated at the time you click on the link so if you really want to get this to work, you would need to write it as an immediately-invoked function expression: 在单击链接时会评估onclick ,因此,如果您真的想使其生效,则需要将其编写为立即调用的函数表达式:

<a href="#" onClick="(function(event){
   event.preventDefault();
   document.getElementById('LogOut-form').submit();
 })(event)">LogOut</a>

But you really should not do it that ways. 但是您真的不应该那样做。 You should always keep html, js and css separated. 您应该始终将html,js和CSS分开。

 <a href="#" onClick=" event.preventDefault(); document.getElementById('LogOut-form').submit(); ">LogOut</a> 

or 要么

 function sendData (event) { event.preventDefault(); document.getElementById('LogOut-form').submit(); } 
 <a href="#" onClick="sendData()">LogOut</a> 

The context you are trying to use the function expression in expects the function keyword to start a function declaration. 您尝试在其中使用函数表达式的上下文期望function关键字启动函数声明。

A function declaration must have a name. 函数声明必须具有名称。

There is no point in putting a function there anyway: You never call it. 无论如何,在其中放置一个函数毫无意义:您永远不会调用它。

The value of an onclick attribute is the function body. onclick属性的值是函数主体。

If you were going to go down this route then you code would look like this: 如果您打算沿着这条路线走,那么您的代码将如下所示:

onclick="event.preventDefault(); document.getElementById('LogOut-form').submit();"

But don't do that! 但是不要那样做!


Intrinsic event attributes are horrible for a variety of reasons. 内在事件属性由于多种原因而令人恐惧。 Bind your event handlers with JavaScript. 将事件处理程序与JavaScript绑定。

<a href="#">LogOut</a>

document.querySelector("a").addEventListener("click", function(event){
   event.preventDefault();
   document.getElementById('LogOut-form').submit();
});

But don't do that either! 但是也不要这样做!


You have a link to the top of the page which you are then "enhancing" with JavaScript to submit a form. 您具有指向页面顶部的链接,然后使用JavaScript“增强”该表单来提交表单。

If the JavaScript fails, it doesn't make any sense to link to the top of the page. 如果JavaScript失败,则链接到页面顶部没有任何意义。

Before applying JavaScript: Pick the HTML with the most appropriate semantics and default functionality . 在应用JavaScript之前: 选择具有最适当语义和默认功能的HTML

In this case, that means use a submit button . 在这种情况下,这意味着使用提交按钮

<button>LogOut</button>

If you don't like the way it looks, apply CSS to style it the way you want. 如果您不喜欢它的外观,请应用CSS对其进行样式设置。

 button { text-decoration: underline; color: blue; background: none; border: none; padding: 0; display: inline; } 
 <button>LogOut</button> 

you're declaring function inside onClick and you should call it. 您在onClick声明了函数,应该调用它。 Define your function outside and then call it from onClick like myFunction() . 在外部定义函数,然后像myFunction()一样从onClick调用它。

I think that you can't call a function directly that way, cause that code is trying to create a new function, not executing that function. 我认为您不能以这种方式直接调用函数,因为该代码试图创建一个新函数,而不是执行该函数。 You should create a function somewhere, and call it at the onclick event. 您应该在某个地方创建一个函数,并在onclick事件中调用它。 For instance: 例如:

    function myFunction(evt) {
        evt.preventDefault();
        document.getElementById('LogOut-form').submit();
    }

    <a href="#" onClick="myFunction">LogOut</a>

If you want to bind a click event directly to an HTML element, then you should use a standard method like this: 如果要将click事件直接绑定到HTML元素,则应使用如下所示的标准方法:

<html>
   <head>
   </head>
   <body>
     <form id="LogOut-form" type="submit"></form>
     <a href="#" onClick="buttonClicked(event)">LogOut</a>
   </body>
   <script>
     function buttonClicked(event){
       event.preventDefault();
       document.getElementById('LogOut-form').submit();
     }
   </script>
</html>

Here, buttonClicked() is a function, which will invoke whenever click triggered. 在此, buttonClicked()是一个函数,只要单击触发就将调用该函数。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM