jQuery - after() 函数触发两次
这是我的代码片段:
$.ctrl('J', function() {
$("#"+currentId).after('<div contentEditable="true">test</div>');
});
运行后,将添加 2 个带有“test”的 div,而不是 1 个。
我缺少什么?
这是 CTRL 功能:
$.ctrl = function(key, callback, args) {
var isCtrl = false;
$(document).keydown(function(e) {
if (!args) args = [];
if (e.ctrlKey) isCtrl = true;
if (e.keyCode == key.charCodeAt(0) && isCtrl) {
callback.apply(this, args);
return false;
}
}).keyup(function(e) {
if (e.ctrlKey) isCtrl = false;
});
};
先谢谢您。
问候
编辑:
也许与此有关:
$('div[id|="edid"]').focus(function() {
$('div[id|="edid"]').removeClass('onFocus');
$(this).addClass('onFocus');
var currentId = $(this).attr('id');
});
This is my code snippet:
$.ctrl('J', function() {
$("#"+currentId).after('<div contentEditable="true">test</div>');
});
After running it, 2 divs with "test" will be added instead of 1.
What am I missing?
This is the CTRL function:
$.ctrl = function(key, callback, args) {
var isCtrl = false;
$(document).keydown(function(e) {
if (!args) args = [];
if (e.ctrlKey) isCtrl = true;
if (e.keyCode == key.charCodeAt(0) && isCtrl) {
callback.apply(this, args);
return false;
}
}).keyup(function(e) {
if (e.ctrlKey) isCtrl = false;
});
};
Thank you in advance.
Greetings
Edit:
maybe it has something to do with this:
$('div[id|="edid"]').focus(function() {
$('div[id|="edid"]').removeClass('onFocus');
$(this).addClass('onFocus');
var currentId = $(this).attr('id');
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我也遇到过类似的问题。也许你使用的是旧的 jquery 版本?
JQuery 1.4.4 中的新增功能
I've had a similar problem. May be you are using an old jquery version?
What’s New in JQuery 1.4.4
您可能在 HTML 代码中包含两次 jQuery。像这样:
You're probably including jQuery two times in your HTML code. Like this: