简体   繁体   English

可扩展的传播

[英]jeditable propagation

i'm using jeditable and I have nested elements all binded to jeditable. 我正在使用jeditable,并且我将嵌套元素全部绑定到可裁剪的。 Problem is when I click on a nested element the click event gets triggered on the top most parent. 问题是,当我点击嵌套元素时,click事件会在最顶层的父元素上触发。 How can I avoid this ? 我怎么能避免这个?

$(document).ready(function() {
 console.log('loading');
 $('div.wrapper').editable('edit/', { 
     loadurl   : 'edit/',
     //id        : 'section',
     name      : 'content',
     submitdata  : function(value, settings) {
         var section = $(this).parent('section').attr("data-section");
         return {
             section: section,
             type: 'ajax',
         }
     },
     loaddata  : function(value, settings) {
         var section = $(this).parent('section').attr("data-section");
         return {
             section: section,
             type: 'ajax',
         }
     },
     rows      : 6,
     width     : '100%',
     type      : 'textarea',
     cancel    : 'Cancel',
     submit    : 'OK',
     indicator : 'Saving changes',
     tooltip   : "Doubleclick to edit...",
     onblur    : 'ignore',
     event     : "dblclick",
     style     : 'inherit',
     callback : function(value, settings) {
         // console.log(this);
         console.log(value);
         console.log(settings);
         $('section[class^="annotation"]').each(function(index) {
            $(this).attr('data-section', index + 1);
         });
     }
 });
});

[edit] [编辑]

Here is the HTML code: 这是HTML代码:

<article>
    <section class="annotation1" data-section="1" id="section_data-section1" typeof="aa:section">
        <div class="wrapper" title="Doubleclick to edit...">
            <h1>Section </h1>
            <p>some content</p>
            <section class="annotation2" data-section="2" id="subsection_data-section2" typeof="aa:section">
                <div class="wrapper" title="Doubleclick to edit...">
                    <h2>Subsection </h2>
                    <p>some more content</p>
                </div>
            </section>
        </div>
    </section>
</article>

Thanks! 谢谢!

This is trickier than I originally thought... 这比我原先想象的要棘手......

Firstly, you could handle the .dblclick event for the div.wrapper , so you can stop event propagation. 首先,你可以处理.dblclick的事件div.wrapper ,这样你就可以停止事件传播。 At each double-click, you attach the jEditable to the element and trigger it (note the .click() after calling .editable() . After finish editing the element, destroy the jEditable element. 在每一个双击,你附加jEditable的元素和触发它(注意.click()调用后.editable()完成编辑元素后,破坏jEditable元素。

While I was thinking it was the end of it, something trickier came up. 虽然我认为这是它的结束,但是出现了一些棘手的问题。 When finishing editing the outer div.wrapper element, the dblclick event in the inner div.wrapper disappeared! 当完成编辑外div.wrapper元素,在dblclick在内部事件div.wrapper消失了! So, the div.wrapper element has to be cloned before it becomes an editable element. 因此, div.wrapper元素必须在它成为可编辑元素之前克隆。 And just after the jEditable restores the wrapper element, it is replaced with the previously stored clone. 在jEditable恢复包装元素之后,它将被先前存储的克隆替换。

$('div.wrapper').dblclick(function(event) {
    event.stopPropagation();

    // save a clone of "this", including all events and data
    $(this).data('clone', $(this).clone(true, true))
        .editable('edit/', {
        onreset: function() {
            var $that = this.parent();
            $that.editable('destroy');

            // restore the editable element with the clone
            // to retain the events and data
            setTimeout(function() {
                $that.replaceWith($that.data('clone'));
            }, 50);
        }
    }).click();
});

See it in action: http://jsfiddle.net/william/vmdz6/3/ . 看到它在行动: http//jsfiddle.net/william/vmdz6/3/

You may need to manually update the cloned element after it is updated with the edited data. 在使用编辑的数据更新克隆元素后,您可能需要手动更新克隆元素。 You should be able to do that in the callback function. 您应该能够在callback函数中执行此操作。

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

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