可编辑传播
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这比我最初想象的要棘手......
首先,您可以处理
div.wrapper
的.dblclick
事件,这样您就可以停止事件传播。每次双击时,您都会将 jEditable 附加到元素并触发它(请注意调用.editable()
后的.click()
。完成编辑元素后,销毁 jEditable 元素时,当完成编辑外部
div.wrapper
元素时,出现了一些棘手的情况。内div.wrapper
消失了!因此,div.wrapper
元素必须在成为可编辑元素之前被克隆,并且在 jEditable 恢复包装元素之后,它就会被替换。与之前存储的克隆一起查看它的实际效果:http://jsfiddle.net/william/vmdz6/3/。
您可能需要在使用编辑的数据更新克隆元素后手动更新它。在
回调
函数中。This is trickier than I originally thought...
Firstly, you could handle the
.dblclick
event for thediv.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, thedblclick
event in the innerdiv.wrapper
disappeared! So, thediv.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.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.