jQuery 当函数已经被其他事件调用时停止调用函数
jQuery stop function from being called when its already being called by other event
我正在尝试以两种方式调用一个函数:无论哪种方式,调用该函数时都不应允许另一个调用该方法
这里 renameFolder
在两个地方被调用:第一个是在 focus out 上,第二个是在按下 enter 时。
问题是当按下 enter 时,它会调用该函数 2 次,1 次用于按下 enter,2 次用于聚焦。
以下是我的 jsfiddle 示例:https://jsfiddle.net/tzx0ojLk/12/
下面是我的代码:
$('#rename').click(function () {
//delete previous popup-menu if exists
removeMenu();
var folder = e.target.id;
var folderId = $("#" + folder).attr("id");
var folderName = $('#' + folder).parent().parent().children('.bd-title').text().trim();
$('#' + folder).parent().parent().children('.bd-title').replaceWith('<textarea class="bd-folder-title-input" oninput="this.style.height = `1px`;this.style.height = (10+this.scrollHeight)+`px`;" onfocusout="renameFolder()" maxlength="50" cols="10">' + folderName + '</textarea>');
$('textarea').focus();
$('textarea').select();
$("textarea").keypress(function (e) {
if (e.which == 13 && !e.shiftKey) {
// alert(e);
// $('textarea').trigger( "focusout" );
// console.log(e);
// e.trigger('focusout');
// $('#' + folder).parent().parent().children('.bd-folder-title-input').trigger( "focusout" );
$('body').focus();
// console.log($(e.currentTarget));
$('.bd-folder-title-input').trigger('focusout');
}
});
});
调用 focus 后添加 focusout listener 即可 select:
$('#rename').click(function() {
//delete previous popup-menu if exists
var folder = 1;
//alert(folder);
var folderId = $("#" + folder).attr("id");
var folderName = $('#' + folder).parent().parent().children('.bd-title').text().trim();
$('#' + folder).parent().parent().children('.bd-title').replaceWith('<textarea class="bd-folder-title-input" oninput="this.style.height = `1px`;this.style.height = (10+this.scrollHeight)+`px`;" maxlength="50" cols="10">' + folderName + '</textarea>');
$('textarea').focus();
$('textarea').select();
$('textarea').on('focusout', function(event) {
renameFolder()
});
$("textarea").keypress(function(e) {
if (e.which == 13 && !e.shiftKey) {
renameFolder();
}
});
});
在 playground 中查看:https://jsfiddle.net/denisstukalov/espvc8t4/1/#&togetherjs=5mdJ3oeu1y
我正在尝试以两种方式调用一个函数:无论哪种方式,调用该函数时都不应允许另一个调用该方法
这里 renameFolder
在两个地方被调用:第一个是在 focus out 上,第二个是在按下 enter 时。
问题是当按下 enter 时,它会调用该函数 2 次,1 次用于按下 enter,2 次用于聚焦。
以下是我的 jsfiddle 示例:https://jsfiddle.net/tzx0ojLk/12/
下面是我的代码:
$('#rename').click(function () {
//delete previous popup-menu if exists
removeMenu();
var folder = e.target.id;
var folderId = $("#" + folder).attr("id");
var folderName = $('#' + folder).parent().parent().children('.bd-title').text().trim();
$('#' + folder).parent().parent().children('.bd-title').replaceWith('<textarea class="bd-folder-title-input" oninput="this.style.height = `1px`;this.style.height = (10+this.scrollHeight)+`px`;" onfocusout="renameFolder()" maxlength="50" cols="10">' + folderName + '</textarea>');
$('textarea').focus();
$('textarea').select();
$("textarea").keypress(function (e) {
if (e.which == 13 && !e.shiftKey) {
// alert(e);
// $('textarea').trigger( "focusout" );
// console.log(e);
// e.trigger('focusout');
// $('#' + folder).parent().parent().children('.bd-folder-title-input').trigger( "focusout" );
$('body').focus();
// console.log($(e.currentTarget));
$('.bd-folder-title-input').trigger('focusout');
}
});
});
调用 focus 后添加 focusout listener 即可 select:
$('#rename').click(function() {
//delete previous popup-menu if exists
var folder = 1;
//alert(folder);
var folderId = $("#" + folder).attr("id");
var folderName = $('#' + folder).parent().parent().children('.bd-title').text().trim();
$('#' + folder).parent().parent().children('.bd-title').replaceWith('<textarea class="bd-folder-title-input" oninput="this.style.height = `1px`;this.style.height = (10+this.scrollHeight)+`px`;" maxlength="50" cols="10">' + folderName + '</textarea>');
$('textarea').focus();
$('textarea').select();
$('textarea').on('focusout', function(event) {
renameFolder()
});
$("textarea").keypress(function(e) {
if (e.which == 13 && !e.shiftKey) {
renameFolder();
}
});
});
在 playground 中查看:https://jsfiddle.net/denisstukalov/espvc8t4/1/#&togetherjs=5mdJ3oeu1y