admin管理员组文章数量:1221920
This works in clearing in input after a user submits it via ajax on a single input form.
var tweet_input = document.getElementById( 'tweet_input' );
tweet_input.value='';
tweet_input.blur();
However on a 3 input field orm this does not:
var input_url = document.getElementById( 'bookmark_input_url' );
input_url.value='';
var input_title = document.getElementById( 'bookmark_input_title' );
input_title.value='';
var input_tag = document.getElementById( 'bookmark_input_tag' );
input_tag.value='';
input_title.blur();
input_url.blur();
input_tag.blur();
Only the last element is actually blurred. Not sure what is going on here with the other two or how to troubleshoot.
Basically I have Event Listeners that fire on a blur(), they work fine on an actual user blur(), but when I try to initiate them programmatically only one works.
This works in clearing in input after a user submits it via ajax on a single input form.
var tweet_input = document.getElementById( 'tweet_input' );
tweet_input.value='';
tweet_input.blur();
However on a 3 input field orm this does not:
var input_url = document.getElementById( 'bookmark_input_url' );
input_url.value='';
var input_title = document.getElementById( 'bookmark_input_title' );
input_title.value='';
var input_tag = document.getElementById( 'bookmark_input_tag' );
input_tag.value='';
input_title.blur();
input_url.blur();
input_tag.blur();
Only the last element is actually blurred. Not sure what is going on here with the other two or how to troubleshoot.
Basically I have Event Listeners that fire on a blur(), they work fine on an actual user blur(), but when I try to initiate them programmatically only one works.
Share Improve this question edited Nov 27, 2019 at 17:37 Cœur 38.7k26 gold badges202 silver badges277 bronze badges asked May 23, 2012 at 15:22 user656925user656925 1 |2 Answers
Reset to default 16Needs to have focus first.
input_title.focus();
input_title.blur();
...
This works fine in my case:
const fireEvent = (element, eventType="blur") => element && element.dispatchEvent(new Event(eventType));
Now we can call this function from wherever we want to fire any event attached to any element.
fireEvent(input_title)
Reference
本文标签: javascriptblur() does not work programmaticallybut does when initiated from the userStack Overflow
版权声明:本文标题:javascript - blur() does not work programmatically | but does when initiated from the user - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739293413a2156796.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
blur()
is the opposite offocus()
. There can only be one element with focus, therefore you can only blur the last focussed element. What are you trying to achieve here? – Diodeus - James MacFarlane Commented May 23, 2012 at 15:25