什么更有效:jquery中的监听器或函数

时间:2022-10-17 23:40:09

I have been using Jquery alot lately an was wondering if I should use listeners or functions for my clicks.

我最近一直在使用Jquery,我想知道是否应该为我的点击使用监听器或函数。

Normally I do the following:

通常我会做以下事情:

<head>   
  <script type="text/javascipt">
    function buttonclicked() {
    alert("You Clicked it");
  }
  </script>
</head>


<button onclick="buttonclicked()">Click Me</button>

3 个解决方案

#1


2  

Benefits of using listeners also allow you to separate your markup and JavaScript.

使用侦听器的好处还允许您分离标记和JavaScript。

#2


2  

Reasons that it's better to use the jQuery event mechanism and not the "intrusive" onfoo element attributes:

使用jQuery事件机制而不是“侵入性”onfoo元素属性更好的原因:

  • You can take advantage of the "live" mechanism. If your interest is efficiency, then this is something you definitely should be thinking about.
  • 您可以利用“实时”机制。如果你的兴趣是效率,那么这是你绝对应该考虑的事情。
  • Disjoint pieces of code can bind handlers to the same elements for different reasons without even having to know about eachother. You don't have to update a single handler to deal with everything.
  • 不相交的代码片段可以出于不同的原因将处理程序绑定到相同的元素,而不必彼此了解。您不必更新单个处理程序来处理所有事情。
  • You keep your markup much cleaner, and save a lot of trouble when event handling needs to change. If you bind handlers based on (for example) element class ("button.showHelpText" for example) then radical changes to your "help text" code can be made without having to touch the HTML markup at all.
  • 您可以使标记更清晰,并在事件处理需要更改时节省大量麻烦。如果基于(例如)元素类(例如“button.showHelpText”)绑定处理程序,则可以根据需要对“帮助文本”代码进行根本性更改,而无需触摸HTML标记。
  • You avoid the unsightly mess of polluting the global Javascript namespace with all those global handler functions.
  • 您可以避免使用所有这些全局处理函数来污染全局Javascript命名空间。
  • You have solid, dependable access to the "event" data, courtesy of the jQuery framework.
  • 您可以通过jQuery框架获得对“事件”数据的可靠,可靠的访问。

Overall, if you're going to use jQuery, dive in and really use it.

总的来说,如果您打算使用jQuery,请深入了解并真正使用它。

#3


1  

i think its more efficient to do what you did in your example.

我认为你在你的例子中做的更有效率。

Also, i wouldnt do that since it is not that much more efficient that it would matter, and it makes for ugly code. Also its better to separate your js from your html.

此外,我不会这样做,因为它不是那么重要,它会重要,它会造成丑陋的代码。最好将你的js与你的html分开。

So please dont do what you did in your example unless you have a very good reason

所以,除非你有充分的理由,否则请不要在你的例子中做你做过的事

#1


2  

Benefits of using listeners also allow you to separate your markup and JavaScript.

使用侦听器的好处还允许您分离标记和JavaScript。

#2


2  

Reasons that it's better to use the jQuery event mechanism and not the "intrusive" onfoo element attributes:

使用jQuery事件机制而不是“侵入性”onfoo元素属性更好的原因:

  • You can take advantage of the "live" mechanism. If your interest is efficiency, then this is something you definitely should be thinking about.
  • 您可以利用“实时”机制。如果你的兴趣是效率,那么这是你绝对应该考虑的事情。
  • Disjoint pieces of code can bind handlers to the same elements for different reasons without even having to know about eachother. You don't have to update a single handler to deal with everything.
  • 不相交的代码片段可以出于不同的原因将处理程序绑定到相同的元素,而不必彼此了解。您不必更新单个处理程序来处理所有事情。
  • You keep your markup much cleaner, and save a lot of trouble when event handling needs to change. If you bind handlers based on (for example) element class ("button.showHelpText" for example) then radical changes to your "help text" code can be made without having to touch the HTML markup at all.
  • 您可以使标记更清晰,并在事件处理需要更改时节省大量麻烦。如果基于(例如)元素类(例如“button.showHelpText”)绑定处理程序,则可以根据需要对“帮助文本”代码进行根本性更改,而无需触摸HTML标记。
  • You avoid the unsightly mess of polluting the global Javascript namespace with all those global handler functions.
  • 您可以避免使用所有这些全局处理函数来污染全局Javascript命名空间。
  • You have solid, dependable access to the "event" data, courtesy of the jQuery framework.
  • 您可以通过jQuery框架获得对“事件”数据的可靠,可靠的访问。

Overall, if you're going to use jQuery, dive in and really use it.

总的来说,如果您打算使用jQuery,请深入了解并真正使用它。

#3


1  

i think its more efficient to do what you did in your example.

我认为你在你的例子中做的更有效率。

Also, i wouldnt do that since it is not that much more efficient that it would matter, and it makes for ugly code. Also its better to separate your js from your html.

此外,我不会这样做,因为它不是那么重要,它会重要,它会造成丑陋的代码。最好将你的js与你的html分开。

So please dont do what you did in your example unless you have a very good reason

所以,除非你有充分的理由,否则请不要在你的例子中做你做过的事