jQuery 使用 live() 函数进行就地编辑的问题..需要忍者

发布于 2024-11-28 17:27:04 字数 899 浏览 0 评论 0原文

这可能是一个简单的修复,但我很难理解它......

我正在使用 jQuery 就地编辑插件来处理一些动态生成的 div。应该很简单:点击新创建的div,就可以编辑内容了。我在 live() 方面遇到了问题。

如果不使用 live(),它显然对于静态 div 来说工作得很好。单击一次,获得可编辑的内容。

然而,在使用 live() 时,我需要双击才能编辑内容。然后任何后续的点击,都只需要一次。这有点像焦点问题。也许修改插件本身会有帮助?

这正是我所说的... http://jsfiddle.net/efflux/62CzU /

这与我使用 live 调用 editinplace() 函数的方式有关:

$('.editable').live('click',function() {
    //event.preventDefault();
    $('.editable').editInPlace({
        callback: function(unused, enteredText) { return enteredText; },
        bg_over: "#cff",
        field_type: "textarea",
        textarea_rows: "5",
        textarea_cols: "3",
        saving_image: "./images/ajax-loader.gif"
    });  
 });   

如何让 edit-in-place 插件在我通过 js 创建的 div 上正常运行?

任何帮助将不胜感激!

This is probably an easy fix, but I am having trouble wrapping my brain around it...

I'm using a jQuery edit-in-place plugin for some divs that will be generated on the fly. It should be simple: Click in the newly created div, and be able to edit the contents. I'm running into problems with live().

Without using live(), it obviously works fine for a static div. Click once, get editable contents.

While using live(), however, I need to double click in order to edit the contents. Then any subsequent time it's clicked, it only takes once. It's sorta like a focus issue. Perhaps modifying the plugin itself would help?

Here is exactly what I'm talking about... http://jsfiddle.net/efflux/62CzU/

It has something to do with the way I'm calling the editinplace() function with live:

$('.editable').live('click',function() {
    //event.preventDefault();
    $('.editable').editInPlace({
        callback: function(unused, enteredText) { return enteredText; },
        bg_over: "#cff",
        field_type: "textarea",
        textarea_rows: "5",
        textarea_cols: "3",
        saving_image: "./images/ajax-loader.gif"
    });  
 });   

How can get the edit-in-place plugin to function normally on my divs created via js?

Any help would be appreciated!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

北笙凉宸 2024-12-05 17:27:04

快速而肮脏的修复:http://jsfiddle.net/62CzU/5/

var $this = $(this),
      isInit = $this.data('edit-in-place');
if(!isInit){
    $('.editable').editInPlace({
        callback: function(unused, enteredText) { return enteredText; },
        bg_over: "#cff",
        field_type: "textarea",
        textarea_rows: "5",
        textarea_cols: "3",
        saving_image: "./images/ajax-loader.gif"
    });
    $this.click();
}

Quick and dirty fix: http://jsfiddle.net/62CzU/5/

var $this = $(this),
      isInit = $this.data('edit-in-place');
if(!isInit){
    $('.editable').editInPlace({
        callback: function(unused, enteredText) { return enteredText; },
        bg_over: "#cff",
        field_type: "textarea",
        textarea_rows: "5",
        textarea_cols: "3",
        saving_image: "./images/ajax-loader.gif"
    });
    $this.click();
}
凉月流沐 2024-12-05 17:27:04

它不起作用,因为在您单击它之前它尚未设置。单击它后,您就设置了需要单独单击的 EditInPlace。设置后只需再次点击即可: http://jsfiddle.net/62CzU/6/

It doesn't work because it's not set up until you click on it. Once you click on it, you set up the EditInPlace which requires it's own click. Just trigger another click after you set it up: http://jsfiddle.net/62CzU/6/

梦里泪两行 2024-12-05 17:27:04

现场演示

只需将按钮单击更改为此即可。

$("button.btn").click(function() {
    $(".mydiv").prepend('<div class="passage-marker"><div class="annotation editable">it take 2 clicks to edit me, unless I have already been edited</div></div>');

         $('.editable').editInPlace({
            callback: function(unused, enteredText) { return enteredText; },
            bg_over: "#cff",
            field_type: "textarea",
            textarea_rows: "5",
            textarea_cols: "3",
            saving_image: "./images/ajax-loader.gif"
        });  

    });
}

基本上,您只是在每次创建它时重新绑定它。您遇到 live 问题的原因是,第一次单击时它绑定了它,所以第二次单击时它已经绑定并且可以工作。

Live Demo

Just change the buttons click to this.

$("button.btn").click(function() {
    $(".mydiv").prepend('<div class="passage-marker"><div class="annotation editable">it take 2 clicks to edit me, unless I have already been edited</div></div>');

         $('.editable').editInPlace({
            callback: function(unused, enteredText) { return enteredText; },
            bg_over: "#cff",
            field_type: "textarea",
            textarea_rows: "5",
            textarea_cols: "3",
            saving_image: "./images/ajax-loader.gif"
        });  

    });
}

Basically you are just rebinding it every time you create it. The reason you had an issue with live is because on the first click it bound it, so then on the second click it was already bound and would work.

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