如果它们是相同的,那么为什么会有这样的事件呢?
谁能告诉我之间的区别blur和focusout,focus并focusin用一个简单的例子吗?
我有一个这样的清单:
<ul>
<li class="item">Item 1</li>
<li class="item">Item 2</li>
<li class="item">Item 3</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
我需要将活动类分别添加到每个项目中,并在单击时将其从上一个项目中删除。
类似于这个问题Vanilla JS remove class from previous selection但是由于旧的浏览器兼容性,我需要使用 For 循环而不是 ForEach 。
我一直在努力使这个答案适应我的例子:
const items = document.querySelectorAll(".item");
for (let i = 0; i < items.length; i++) {
const item = items[i];
item.addEventListener("click", addActiveClass);
for (let i = 0; i < items.length; i++) {
const item = items[i];
item.addEventListener("click", removeClass);
}
}
function removeClass(e) {
e.target.classList.remove("active");
}
function addActiveClass(e) {
e.target.classList.add("active");
}
Run Code Online (Sandbox Code Playgroud)
但它仍然没有按预期工作:(
我目前正在使用工具在jQuery事件按键上检索数据库中的地址.当文本输入失去焦点时,我会取消每个待处理的请求,以避免在用户完成填写输入后出现下拉列表.一切正常,但是,完成我发送一个最终的ajax请求转到我的表单的下一步.这个请求比取消所有这些请求之前要慢得多.我不明白为什么,取消的请求不应该影响待处理的请求(我确定它们已被取消,在Chrome工具的网络选项卡中查找).我正在使用此代码:
jQuery.xhrPool = [];
jQuery.xhrPool.abortAll = function() {
jQuery(this).each(function(idx, jqXHR) {
jqXHR.abort();
jQuery('.loader').hide();
});
};
jQuery.ajaxSetup({
beforeSend: function(jqXHR) {
jQuery.xhrPool.push(jqXHR);
},
complete: function(jqXHR) {
var index = jQuery.xhrPool.indexOf(jqXHR);
if (index > -1) {
jQuery.xhrPool.splice(index, 1);
}
}
});
jQuery('#my_input').blur(function(){
jQuery.xhrPool.abortAll();
});
Run Code Online (Sandbox Code Playgroud)
我想我有一个我没有得到的优化技巧.谢谢你的帮助.
我刚刚了解了onfocusout,onfocusin但是这些功能似乎不起作用。我尝试使用Firefox和Chrome。根本没有控制台消息,即使当我集中精力并松开它时也是如此。我究竟做错了什么?
window.addEventListener
(
"onfocusout",
function()
{
console.log("page lost focus");
}
);
window.addEventListener
(
"onfocusin",
function()
{
console.log("page has focus");
}
);Run Code Online (Sandbox Code Playgroud)
<h1>Test</h1>Run Code Online (Sandbox Code Playgroud)
我有AJAX提交的以下表格:
的HTML
<form class="form-horizontal" id="formEditUsername" action="<?php echo URL; ?>account/editusername_action" method="post">
<input type="text" class="form-control" name="user_name" id="user_name" placeholder="Add username" value="User ">
<button type="submit" class="btn btn-default">Save</button>
</form>
Run Code Online (Sandbox Code Playgroud)
AJAX
$("#formEditUsername").submit(function(event){
//disable default click operation
event.preventDefault();
var action_url = $(this).attr("action");
$(".help-block.username").hide().html("<i class='fa fa-refresh fa-spin'></i> Processing...").fadeIn('slow');
console.log(action_url);
var postData = $(this).serializeArray();
//console.log(postData);
$.post(action_url,postData,function(data){
console.log(data);
var obj = $.parseJSON(data);
$(".help-block.username").html("Saving...");
if(obj.status == "error")
{
$(".help-block.username").css({color: '#990033'});
$(".help-block.username").html(obj.message).fadeIn('slow');
};
if(obj.status == "success")
{
$(".help-block.username").css({color: '#00b25a'});
$(".help-block.username").html(obj.message).fadeIn('slow');
$(".namebox").html(obj.username);
}
});
});
Run Code Online (Sandbox Code Playgroud)
我想添加一个功能,以便在输入(#user_name)失去焦点之后,表单使用focusout()提交。有任何想法吗?
javascript ×5
jquery ×4
ajax ×2
dom ×1
events ×1
for-loop ×1
foreach ×1
html ×1
optimization ×1