简体   繁体   English

jQuery,链接/按钮的侦听器和文档就绪问题

[英]Jquery, listeners to links/buttons and document ready issue

I'm sorry, if my question is very simple, but can't understand it. 很抱歉,如果我的问题很简单,但却无法理解。 As I understand it's good practice to attach links/buttons listeners via js but not html. 据我了解,通过js(而非html)附加链接/按钮侦听器是一种很好的做法。 I mean this is bad practice: 我的意思是不好的做法:

<a href="javascript:" onclick="temp();">

And this is good 这很好

<a href="javascript:" id="mylink">
in js code
$("#mylink").on...

As I understand the main idea is that html page mustn't contain any javascript code, and all js code must be in js file. 据我了解,主要思想是html页面不得包含任何javascript代码,并且所有js代码都必须位于js文件中。 Ok, I like this idea. 好吧,我喜欢这个主意。

The problem I see, implementing such approach is when to attach such listeners. 我看到的问题是,实施这种方法是何时附加此类侦听器。

All I found - is that listeners can be attach only when document is ready (otherwise we can't add listeners to elements that are not ready). 我发现的所有内容是,仅当文档准备就绪时才可以附加侦听器(否则,我们无法将侦听器添加到未准备就绪的元素中)。

However, if I have a page that shows for example 100 rows from database, then its loading is not fast (at comparison to short pages). 但是,如果我有一个页面显示例如来自数据库的100行,那么它的加载速度并不快(与短页面相比)。

And if we wait until document is ready, the user can't work with site. 而且,如果我们等到文档准备好,用户将无法使用网站。

Please correct me if I'm wrong or say how can we make it to work with long pages without such things as timeouts. 如果我写错了,请纠正我,或者说我们如何使它在处理长页面时没有超时等问题。

You can use delegated events : Instead of waiting for an element to be created/ready and then attaching an event handler, the handler is attached to a parent element, that exists early, such as the document object, since that always exists. 您可以使用委托事件 :无需等待创建/准备就绪的元素然后附加事件处理程序,而是将处理程序附加到早期存在的父元素(例如文档对象),因为它始终存在。

Example: Instead of 示例:代替

$('#mylink').on('click', function() { /* do something */ });

it's 它的

$(document).on('click', '#mylink', function() { /* do something */ });

This is normally used when elements are added dynamically, but it works just as well "while the page loads". 这通常在动态添加元素时使用,但在“页面加载时”同样有效。

EDIT: Short explanation: 编辑:简短说明:

Basically DOM events "bubble": When an element is clicked, then the event isn't just sent to that element, but to all of its ancestors, one by one going up the DOM tree. 基本上DOM事件“冒泡”:单击某个元素时,该事件不仅会发送到该元素,而且还会发送到其所有祖先,并逐一发送到DOM树。 The event contains information which element was originally clicked and you could filter/check for the element yourself: 该事件包含最初单击哪个元素的信息,您可以自己过滤/检查该元素:

$(document).on('click', function(event) { 
  if( $(event.target).is('#mylink') ) {
    alert('the link was clicked');
  }
});

Using the selector as the second argument in .on() jQuery does the filtering for you, and sets this to the link instead of the document. 使用选择作为第二个参数.on() jQuery不会过滤你,并将this向链接,而不是文件。

See also this example: http://jsfiddle.net/qhsktpcs/ 另请参见以下示例: http : //jsfiddle.net/qhsktpcs/

Make sure to add all your JS code in this block : 确保在此块中添加所有JS代码:

$( function( ) { 
    $( '#mylink' ).on( 'click' , function ( e ) {
         console.log ( 'link clicked' );
         e.preventDefault();
    });
    // the rest of your code ... 
});

Otherwise, it will try to register click events on elements that are not yet created (since the DOM is not ready yet) and it will wait untill everything's ready ( even if you have ~100 rows to be pulled from the DB like you said ) 否则,它将尝试在尚未创建的元素上注册click events (因为DOM尚未准备好),并且它将等待一切​​准备就绪(即使您要从数据库中提取约100行,如您所说)

** EDIT ** Example here : http://jsfiddle.net/6ctd2hqs/1/ **编辑**示例在这里: http : //jsfiddle.net/6ctd2hqs/1/

You can test if your page is ready, and then start attaching listeners. 您可以测试页面是否准备就绪,然后开始附加侦听器。 Eg for jQuery: 例如jQuery:

$.ready(function() {
  // listeners here
})

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

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