4

私は 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>

ありがとう!

4

1 に答える 1

0

これは当初考えていたよりも厄介です...

まず、 の.dblclickイベントを処理div.wrapperできるので、イベントの伝播を停止できます。ダブルクリックするたびに、jEditable を要素にアタッチしてトリガーします (.click()呼び出し後に注意してください.editable()。要素の編集が終了したら、jEditable 要素を破棄します。

これで終わりだと思っていたら、さらに厄介なことが起こった。div.wrapper外側の要素の編集が終わるdblclickと、内側のイベントが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/

編集されたデータで更新された後、複製された要素を手動で更新する必要がある場合があります。関数でそれを行うことができるはずですcallback

于 2011-09-15T13:56:29.083 に答える