简体   繁体   English

添加事件监听器的最佳实践(javascript,html)

[英]best practice on adding event listeners (javascript, html)

I know I may be asking for the moon here but I'm looking for some experienced opinons on the best way to add event listeners or rather 'When' or 'Where' to add them in the js file. 我知道我可能会在这里要求登月,但我正在寻找一些有关添加事件监听器的最佳方式的经验丰富的意见,或者更确切地说是“何时”或“何处”将它们添加到js文件中。

Take my take as an example. 以我的看法为例。 I have a page which has a bunch of onclick events that now have to be handled by properties in the JS file 我有一个页面,其中有一堆onclick事件,现在必须由JS文件中的属性处理

eg 例如

var test = document.getElementById("i-am-element");
test.onclick = testEventListener;

My question is where exactly I should add this in the js file. 我的问题是我应该在js文件中添加它。

How I was planning to go about it was to have something like the following 我打算如何去做它是有类似以下的东西

$(document).ready(function() {
    var test = document.getElementById("test-me-shane");
    test.onclick = testEventListener;

    //add all the functions to different elements
});

myfunction1(){}
myfunction2(){}
myfunction3(){}

So that once the document is ready, only then are all the event listeners added. 因此,一旦文档准备就绪,只有这样才能添加所有事件监听器。 Is this acceptable or is there are more universally accepted way of doing it. 这是可以接受的还是有更普遍接受的方式。

NOTE: I know this question may appear subjective so I'm going with the correct answer will be the most popular way you've seen seen event listeners added. 注意:我知道这个问题可能看似主观,所以我正确的回答将是你看到的事件听众添加的最流行的方式。 I'm sure there must be a majority acceptance on this and I apologize in advance if its similiar to something like where you should declare variables, at the start or when you need them. 我肯定必须得到多数人的接受,如果它类似于你应该在开始时或需要时声明变量的地方,我会事先道歉。

In Java, should variables be declared at the top of a function, or as they're needed? 在Java中,变量应该声明在函数的顶部,还是需要它们?

You really want to be able to add all your event listeners in the same place; 您真的希望能够在同一个地方添加所有事件监听器; why? 为什么? Simply for ease-of-maintenance. 只是为了便于维护。

Because of this, the best place to put all your event listeners is in a place where you can guarantee all elements you'll possibly want to bind event handlers to are available. 因此,放置所有事件侦听器的最佳位置是您可以保证可能需要绑定事件处理程序的所有元素。

This is why the most common place to bind your event handlers is after the DOMReady event has fired $(document).ready() . 就是绑定事件处理程序的最常见位置是在DOMReady事件触发$(document).ready()

As always, there are some exceptions to the rule. 与往常一样,规则一些例外。 Very occasionally, you might want to bind an event handler to an element as soon as it is available; 很偶然,你可能要绑定的事件处理程序,尽快 ,因为它是可用的元素; which is after the closing tag of the element has been defined. 这是在元素的结束标记之后定义的。 In this instance, the following snippet should be used: 在这种情况下,应使用以下代码段:

<div id="arbitrary-parent">
    <h1 id="arbitrary-element">I need an event handler bound to me <strong>immediately!</strong></h1>
    <script>document.getElementById("arbitrary-element").onclick = function () { alert("clicked"); }</script>
</div>

The other thing you should consider is how you are going to bind your handlers. 您应该考虑的另一件事是如何绑定处理程序。 If you stick to: DOMElement.onclick = function () { }; 如果坚持: DOMElement.onclick = function () { }; , you're limiting yourself to binding on handler per event. ,你限制自己绑定每个事件的处理程序。

Instead, the following approach allows you to bind multiple handlers per event: 相反,以下方法允许您为每个事件绑定多个处理程序:

function bind(el, evt, func) {
    if (el.addEventListener){
        el.addEventListener(evt, func, false);
    } else if (el.attachEvent) {
        el.attachEvent('on' + evt, func);
    }
}

Is there a specific reason why you don't simply specify the association when you declare the element in the html 当您在html中声明元素时,是否有一个特定的原因,您不能简单地指定关联
<someTag id="i-am-an-element" onclick="functionToTheEventToExecute()"> </someTag>
I guess so. 大概吧。

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

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