1. <tfoot id='FsuDH'></tfoot>

        <bdo id='FsuDH'></bdo><ul id='FsuDH'></ul>

        <small id='FsuDH'></small><noframes id='FsuDH'>

        <i id='FsuDH'><tr id='FsuDH'><dt id='FsuDH'><q id='FsuDH'><span id='FsuDH'><b id='FsuDH'><form id='FsuDH'><ins id='FsuDH'></ins><ul id='FsuDH'></ul><sub id='FsuDH'></sub></form><legend id='FsuDH'></legend><bdo id='FsuDH'><pre id='FsuDH'><center id='FsuDH'></center></pre></bdo></b><th id='FsuDH'></th></span></q></dt></tr></i><div id='FsuDH'><tfoot id='FsuDH'></tfoot><dl id='FsuDH'><fieldset id='FsuDH'></fieldset></dl></div>
      1. <legend id='FsuDH'><style id='FsuDH'><dir id='FsuDH'><q id='FsuDH'></q></dir></style></legend>
      2. 事件不适用于动态创建的元素

        时间:2023-09-05

        <small id='unY1l'></small><noframes id='unY1l'>

          <tbody id='unY1l'></tbody>
        <i id='unY1l'><tr id='unY1l'><dt id='unY1l'><q id='unY1l'><span id='unY1l'><b id='unY1l'><form id='unY1l'><ins id='unY1l'></ins><ul id='unY1l'></ul><sub id='unY1l'></sub></form><legend id='unY1l'></legend><bdo id='unY1l'><pre id='unY1l'><center id='unY1l'></center></pre></bdo></b><th id='unY1l'></th></span></q></dt></tr></i><div id='unY1l'><tfoot id='unY1l'></tfoot><dl id='unY1l'><fieldset id='unY1l'></fieldset></dl></div>
        <tfoot id='unY1l'></tfoot>
        1. <legend id='unY1l'><style id='unY1l'><dir id='unY1l'><q id='unY1l'></q></dir></style></legend>

              • <bdo id='unY1l'></bdo><ul id='unY1l'></ul>

                  本文介绍了事件不适用于动态创建的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在努力弄清楚为什么 mouseover 事件不能与 .on 处理程序一起使用,该处理程序是从 ajax 动态创建的元素.似乎唯一可行的是带有 .live 的代码,但我知道它已被弃用.

                  $(".dropdown ul li").live("mouseover", function() {alert('鼠标悬停工作');});

                  但是,当我尝试使用 .on 时,它不起作用 - 无论我尝试什么变体(document.ready、.mouseover 等)

                  $(".dropdown ul li").on("mouseover", function() {alert('鼠标悬停工作');});

                  事件处理程序位于代码的底部,因此它们最后执行.有人知道我做错了什么吗?

                  解决方案

                  .on 用于具有动态事件委托 http://api.jquery.com/on/ - 你的主选择器是一个存在的static parent:

                  $(".static-parent").on("event1 event2", ".dynamic-child", function() {

                  或者在你的情况下:

                  $(".dropdown").on("mouseover", "li", function() {alert('鼠标悬停工作!!!!!!!!!');});

                  <块引用>

                  委托事件的优点是它们可以处理来自以后添加到文档的后代元素的事件.通过选择在附加委托事件处理程序时保证存在的元素,您可以使用委托事件来避免频繁附加和删除事件处理程序的需要.例如,该元素可以是模型-视图-控制器设计中视图的容器元素,或者如果事件处理程序想要监视文档中的所有冒泡事件,则可以是文档.在加载任何其他 HTML 之前,文档元素在文档的头部是可用的,因此在此处附加事件是安全的,而无需等待文档准备好.

                  还要确保使用 DOM 就绪 函数

                  jQuery(function($) {//DOM 现已准备就绪,$ 别名已保护$(".dropdown").on("mouseover", "li", function() {alert('鼠标悬停工作!!!!!!!!!');});});

                  I'm pulling my hair out trying to figure out why the mouseover event won't work with the .on handler with a dynamically created element from ajax. The only thing that seems to work is the code with .live but I understand that it is deprecated.

                  $(".dropdown ul li").live("mouseover", function() {
                  alert('mouseover works');
                  });
                  

                  However, when I try using .on, it will not work - no matter what variations I try (document.ready, .mouseover, etc etc)

                  $(".dropdown ul li").on("mouseover", function() {
                  alert('mouseover works');
                  });
                  

                  The event handlers are at the bottom of the code, so they are executed last. Anyone have an idea of what I'm doing wrong??

                  解决方案

                  Using .on for newly generated elements with dynamic event delegation http://api.jquery.com/on/ - where your main selector is an existent static parent:

                  $(".static-parent").on("event1 event2", ".dynamic-child", function() {
                  

                  or in your case:

                  $(".dropdown").on("mouseover", "li", function() {
                     alert('mouseover works!!!!!!!!!');
                  });
                  

                  Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. By picking an element that is guaranteed to be present at the time the delegated event handler is attached, you can use delegated events to avoid the need to frequently attach and remove event handlers. This element could be the container element of a view in a Model-View-Controller design, for example, or document if the event handler wants to monitor all bubbling events in the document. The document element is available in the head of the document before loading any other HTML, so it is safe to attach events there without waiting for the document to be ready.

                  Also make sure to use a DOM ready function

                  jQuery(function($) { // DOM is now ready and $ alias secured
                  
                      $(".dropdown").on("mouseover", "li", function() {
                         alert('mouseover works!!!!!!!!!');
                      });
                  
                  });
                  

                  这篇关于事件不适用于动态创建的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:等待 document.body 存在 下一篇:添加多个 onload 处理程序

                  相关文章

                  最新文章

                1. <small id='8ljJY'></small><noframes id='8ljJY'>

                  1. <i id='8ljJY'><tr id='8ljJY'><dt id='8ljJY'><q id='8ljJY'><span id='8ljJY'><b id='8ljJY'><form id='8ljJY'><ins id='8ljJY'></ins><ul id='8ljJY'></ul><sub id='8ljJY'></sub></form><legend id='8ljJY'></legend><bdo id='8ljJY'><pre id='8ljJY'><center id='8ljJY'></center></pre></bdo></b><th id='8ljJY'></th></span></q></dt></tr></i><div id='8ljJY'><tfoot id='8ljJY'></tfoot><dl id='8ljJY'><fieldset id='8ljJY'></fieldset></dl></div>
                  2. <tfoot id='8ljJY'></tfoot>
                      <bdo id='8ljJY'></bdo><ul id='8ljJY'></ul>

                      <legend id='8ljJY'><style id='8ljJY'><dir id='8ljJY'><q id='8ljJY'></q></dir></style></legend>