简体   繁体   English

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

[英]Problem with jQuery edit-in-place with live() function.. need a ninja

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. 我正在使用jQuery即时编辑插件来处理将在运行中生成的一些div。 It should be simple: Click in the newly created div, and be able to edit the contents. 它应该很简单:单击新创建的div,然后可以编辑其内容。 I'm running into problems with live(). 我遇到了live()的问题。

Without using live(), it obviously works fine for a static div. 如果不使用live(),显然对于静态div来说可以正常工作。 Click once, get editable contents. 单击一次,获取可编辑的内容。

While using live(), however, I need to double click in order to edit the contents. 但是,在使用live()时,我需要双击才能编辑内容。 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/ 这正是我在说的... http://jsfiddle.net/efflux/62CzU/

It has something to do with the way I'm calling the editinplace() function with live: 它与我用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"
    });  
 });   

How can get the edit-in-place plugin to function normally on my divs created via js? 如何使就地编辑插件在通过js创建的div上正常运行?

Any help would be appreciated!! 任何帮助,将不胜感激!!

Quick and dirty fix: http://jsfiddle.net/62CzU/5/ 快速而肮脏的修复程序: 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();
}

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. 一旦单击它,就可以设置EditInPlace,这需要它自己单击。 Just trigger another click after you set it up: http://jsfiddle.net/62CzU/6/ 设置后只需触发另一次点击即可: http : //jsfiddle.net/62CzU/6/

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. 您对live产生问题的原因是,第一次单击将其绑定,因此第二次单击已被绑定并且可以使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM