简体   繁体   English

模糊/焦点上的jQuery没有触发动态添加的元素

[英]jQuery on blur / focusout not firing on dynamically added element

I'm dynamically adding an element in jQuery, and upon doing so, I'm focusing on the element. 我在jQuery中动态添加一个元素,在这样做的时候,我专注于元素。 I'm trying to then remove this element on blur . 我试图在blur删除此元素。 However, the blur event or the focusout event don't seem to be firing when the focus is lost. 但是,当焦点丢失时, blur事件或focusout事件似乎不会触发。

Appending the element: 附加元素:

var searchForm = '<input id="header-search" type="text" class="form-control" placeholder="Search">';
$('body').append(searchForm);
$('#header-search').focus();

Binding to the blur() event: 绑定到blur()事件:

$('#header-search').on('blur', function() {
    alert('blur');      
    $(this).remove();       
});

EDIT: I just created a jsFiddle, and it seems to function as I want. 编辑:我刚刚创建了一个jsFiddle,它似乎按我想要的功能。 Strange. 奇怪。 https://jsfiddle.net/danhaswings/cpczLL7g/ https://jsfiddle.net/danhaswings/cpczLL7g/

Just change it to: 只需将其更改为:

$(document).on('blur', '#header-search', function() {
    alert('blur');      
    $(this).remove();       
});

Since your element is being added dynamically and when your code is executed, no DOM element is found with selector #header-search . 由于您的元素是动态添加的,并且在执行代码时,没有找到带有selector #header-search DOM元素。 So, you need to change the code like above to fix for dynamically added elements. 因此,您需要更改上面的代码以修复动态添加的元素。

When trying to bind an event to any 'Dynamically added element', it'd be better to write 当尝试将事件绑定到任何“动态添加元素”时,最好编写

$(document.body).on("blur", "#header-search", function () {
   // do anything you wanna do...
   alert("blur");
   $(this).remove();
});

If your element is appended before adding your event, you need to bind the event to a parent that already exists using $(document).on( eventName, selector, function(){} ); 如果在添加事件之前附加了元素,则需要使用$(document).on( eventName, selector, function(){} );将事件绑定到已存在的父$(document).on( eventName, selector, function(){} ); .

This is Event delegation and you have to use .on() using delegated-events approach 这是事件委派 ,您必须使用委托事件方法使用.on()

 $(document).on('blur', '#header-search', function() { alert('blur'); if ($(this).val() == '' && $searchButton.is(':active') == false) { $(this).remove(); } }); var searchForm = '<input id="header-search" type="text" class="form-control" placeholder="Search STRI...">'; $('body').append(searchForm); $('#header-search').focus(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

The problem was the $searchButton.is(':active') on line 7, the var was not defined. 问题是第7行的$searchButton.is(':active') ,var未定义。

in this fork is working fine https://jsfiddle.net/w5jpypcy/ 在这个叉子工作正常https://jsfiddle.net/w5jpypcy/

In your code you must have added Jquery first and then the HTML like 在你的代码中,你必须先添加Jquery,然后再添加HTML

$('#header-search').on('blur', function() {
alert('blur');
if ($(this).val() == '' && $searchButton.is(':active') == false) {
    $(this).remove();
}
});


var searchForm = '<input id="header-search" type="text" class="form-control"     placeholder="Search STRI...">';
$('body').append(searchForm);
$('#header-search').focus();

that is why it is working in your JSFiddle where you have added Blur event after you created your runtime input tag and not in your environment. 这就是为什么它在你的JSFiddle中工作,你在创建运行时输入标记之后添加了Blur事件,而不是在你的环境中。

Hope this helps. 希望这可以帮助。

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

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