我的网站上有多个带有文本区域的表单。每个文本区域都分配有 sceditor 插件。现在我尝试通过提供快捷方式提交表单。
首先,我为每个编辑器实例分配了一个 keyup 方法:
$('textarea[name="test01"]').sceditor('instance').keyUp(function(e) {
if (e.ctrlKey && e.keyCode == 13) {
console.log(this); // case 1
}
// ...
后来我也找到了内置的方法:
$('textarea[name="test01"]').sceditor('instance').addShortcut('ctrl+enter', submitform);
function submitform() {
console.log(this); // case 2
}
我的目标是在 sceditor DIV(即文本区域)之前获得同级。从那里我可以找到提交按钮。或者我可以沿着 DOM 树向下查找 textarea / sceditor 嵌套的表单。
但是,
console.log(this);
给出了情况 1:节点的 HTML,i。 e.在 Webdeveloper Console 中我可以看到 seditor 的 HTML。对于情况 2,我收到了 seditor 对象。
问题是我无法访问父级。同样在 sceditor 文档 中我看不出有什么办法可以做到这一点。
感谢您的帮助。
添加快捷方式处理程序时,您知道该处理程序将添加到哪个文本区域,以便您可以执行以下操作:
$('textarea[name="test01"]').sceditor('instance').addShortcut('ctrl+enter', function () {
submitform($('textarea[name="test01"]').get(0).form);
});
function submitform(form) {
console.log(form.id);
}
这可以通过一个简单的插件结合一些工厂重写来完成。
sceditor.plugins.shortcut = function ()
{
let editor;
this.init = function ()
{
editor = this;
this.addShortcut('ctrl+enter', function () {
editor.opts.original.form.submit();
});
};
};
const createFn = sceditor.create;
sceditor.create = (textarea, options) => {
options.original = textarea;
createFn(textarea, options);
};