jQuery - after() 函数触发两次

发布于 2024-12-24 00:07:35 字数 909 浏览 4 评论 0原文

这是我的代码片段:

$.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

所有深爱都是秘密 2024-12-31 00:07:35

我也遇到过类似的问题。也许你使用的是旧的 jquery 版本?

JQuery 1.4.4 中的主要错误修复
- 绑定到文档就绪事件的函数现在将触发一次(它
发射了两次)。

JQuery 1.4.4 中的新增功能

I've had a similar problem. May be you are using an old jquery version?

Major Bugfix in JQuery 1.4.4
- A function bound to the document ready event will now fire once (it
was firing twice).

What’s New in JQuery 1.4.4

横笛休吹塞上声 2024-12-31 00:07:35

您可能在 HTML 代码中包含两次 jQuery。像这样:

<script type="text/javascript" src="/scripts/jquery.min.js"></script>
<script type="text/javascript" src="/scripts/jquery.min.js"></script>

You're probably including jQuery two times in your HTML code. Like this:

<script type="text/javascript" src="/scripts/jquery.min.js"></script>
<script type="text/javascript" src="/scripts/jquery.min.js"></script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文