简体   繁体   English

点击事件不会在 jquery 中触发

[英]Click event doesn't fire in jquery

I appended a new element but when I click on it it has no response.我添加了一个新元素,但是当我单击它时它没有响应。

HTML HTML

<button>add element</button>
<div></div>

Javascript Javascript

$(function(){
    $('button').click(function(){
        $('div').append('<span class="x">x</span>');
    });

    $('.x').click(function(){
        alert('fire'); 
        });
});

 $(function() { $('button').click(function() { $('div').append('<span class="x">x</span>'); }); $('div').on('click', '.x', function() { alert('fire'); }); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button>add element</button> <div></div>

Event handlers are bound only to the currently selected elements;事件处理程序仅绑定到当前选定的元素; they must exist on the page at the time your code makes the event binding call.在您的代码进行事件绑定调用时,它们必须存在于页面上。

Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time.委托事件的优点是它们可以处理来自稍后添加到文档的后代元素的事件。

As you are creating elements.当您创建元素时。

You need to use Event Delegation .您需要使用事件委托 You have to use .on() using delegated-events approach.您必须使用.on()使用委托事件方法。

General Syntax一般语法

$(document).on(event, selector, eventHandler);

Ideally you should replace document with closest static container.理想情况下,您应该用最近的静态容器替换document

Example例子

$('div').on('click', '.x', function(){
    alert('fire'); 
});

One cannot bind click events to dynamically generated elements through calling $(".x") when the element does not exist in the document.当文档中不存在元素时,不能通过调用$(".x")将单击事件绑定到动态生成的元素。 One of the solution is to use Event Delegation, which is something like解决方案之一是使用事件委托,类似于

$(document).on("click",".x",function(){
// do your work here
})

And the other way to do that is do bind the click event to the element when it is generated另一种方法是在生成时将 click 事件绑定到元素

$('button').click(function(){
    $('div').append($("<span>",{
          class: x
    }).text("x").click(function(){
          // do your work here
    }));
});

You're adding the event listener before the item with the x class exists.您在具有 x 类的项目存在之前添加事件侦听器。 You want to add that event listener right after you append that span.您想在附加该跨度后立即添加该事件侦听器。

$(function(){
    $('button').click(function(){
        $('div').append('<span class="x">x</span>');
        $('.x').click(function(){
            alert('fire'); 
        });
    });    
});

You can not bind events directly to dynamically created elements in jQuery.您不能将事件直接绑定到 jQuery 中动态创建的元素。

Use event-delegation to bind to the dynamically created element's parent:使用事件委托绑定到动态创建的元素的父元素:

$(function(){
    $('button').click(function(){
        $('div').append('<span class="x">x</span>');
    });

    $('button').on("click", ".x", (function(){
        alert('fire'); 
        });
});

For more information on this, see: http://api.jquery.com/on/有关这方面的更多信息,请参阅: http : //api.jquery.com/on/

I had another issue why the event was not fired.我还有另一个问题,为什么没有触发该事件。

In my case it was beause of CSS pointer-events was set to none就我而言,这是因为 CSS pointer-events设置为none

https://css-tricks.com/almanac/properties/p/pointer-events/ https://css-tricks.com/almanac/properties/p/pointer-events/

https://developer.mozilla.org/en-US/docs/Web/CSS/pointer-events https://developer.mozilla.org/en-US/docs/Web/CSS/pointer-events

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

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