可编辑传播

发布于 2024-12-04 20:22:16 字数 2049 浏览 0 评论 0原文

我正在使用 jeditable,并且所有嵌套元素都绑定到 jeditable。问题是当我单击嵌套元素时,单击事件会在最顶层的父元素上触发。我怎样才能避免这种情况?

$(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);
         });
     }
 });
});

[编辑]

这是 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>

谢谢!

i'm using jeditable and I have nested elements all binded to jeditable. Problem is when I click on a nested element the click event gets triggered on the top most parent. 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:

<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!

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

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

发布评论

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

评论(1

久伴你 2024-12-11 20:22:16

这比我最初想象的要棘手......

首先,您可以处理 div.wrapper.dblclick 事件,这样您就可以停止事件传播。每次双击时,您都会将 jEditable 附加到元素并触发它(请注意调用 .editable() 后的 .click()。完成编辑元素后,销毁 jEditable 元素时,

当完成编辑外部 div.wrapper 元素时,出现了一些棘手的情况。内div.wrapper 消失了!因此,div.wrapper 元素必须在成为可编辑元素之前被克隆,并且在 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();
});

查看它的实际效果:http://jsfiddle.net/william/vmdz6/3/

您可能需要在使用编辑的数据更新克隆元素后手动更新它。在回调函数中。

This is trickier than I originally thought...

Firstly, you could handle the .dblclick event for the div.wrapper, so you can stop event propagation. 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.

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! So, the div.wrapper element has to be cloned before it becomes an editable element. And just after the jEditable restores the wrapper element, it is replaced with the previously stored clone.

$('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/.

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.

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