简体   繁体   English

Javascript:获取当前元素的同级[CKEditor所需]

[英]Javascript: Get sibling of current element [needed for CKEditor]

Hey, I am working on a simple Plugin for CKEditor, I am not to firm with JavaScript, so here is my problem. 嘿,我正在为CKEditor开发一个简单的插件,我对JavaScript不太了解,所以这是我的问题。

I upload an image and insert it with a dialog, producing something like this. 我上传图像,并在对话框中插入它,生成类似这样的图像。

<div class="float-right image">  
<img alt="alt-text" src="img.jpg" />   
<span class="caption">Caption</span>  
</div>

Works like a charm, but once i is inserted I want to be able to edit it. 就像一个吊饰一样工作,但是一旦我插入,我希望能够对其进行编辑。 Doing so I can get the alt attribute and the src of the image via 这样做,我可以通过获取alt属性和图像的src

var elem = this.getParentEditor().getSelection().getSelectedElement();

and working with getAttribute('...'). 并使用getAttribute('...')。 But I can not figure out how to reach, the caption and the div. 但我不知道如何到达标题和div。

I would be very happy if you could help me. 如果您能帮助我,我将非常高兴。 Thanks in advance. 提前致谢。

BTW: Is there a possibility to use jquery within a plugin for CKEditor? 顺便说一句:是否有可能在CKEditor的插件中使用jquery?

Heres the solution: 这是解决方案:

So what I came up with in the end, thanks to all the help here. 所以最后我要想出什么,这要归功于这里的所有帮助。 In the dialog part: 在对话框部分:

onShow : function()
{
elem = this.getParentEditor().getSelection().getSelectedElement();
var span = elem.getNext();
var parent = span.getParent();
}

this gives me all the stuff I need to fill into the dialog once I open it again. 再次打开对话框后,这将为我提供我需要填写的所有内容。

to submit everything I needed to find out how to change the current selection. 提交我需要的所有内容,以了解如何更改当前选择。 This works like this: 这是这样的:

onOk:
function()
    {
editor.getSelection().selectElement(editor.getSelection().getSelectedElement().getParent());
editor.insertHtml('<div ...</div>');
}

Thank you guys, I only started digging into the CKEditor Plugins today and already I got two nice ones. 谢谢大家,我今天才开始研究CKEditor插件,已经有了两个不错的插件。

通过CKEditor API,您可以使用.getNext()

Yes, you can use jQuery within a CKEditor plugin as long as you don't intend to distribute the plugin (other people may not have jQuery), in which case your solution becomes: 是的,只要您不打算分发插件(其他人可能没有jQuery),就可以在CKEditor插件中使用jQuery,在这种情况下,您的解决方案将变为:

$(elem).next('span') and $(elem).prev('div') $(elem).next('span')$(elem).prev('div')

If you don't want to use jQuery, you have .nextSibling and .previousSibling 如果您不想使用jQuery,则可以使用.nextSibling和.previousSibling

Note, however, that the next and prev siblings will often be textNodes depending on what browser you're in. So to get the span, you need a loop: 但是请注意,下一个和上一个兄弟姐妹通常是textNode,具体取决于您所使用的浏览器。因此,要获取跨度,您需要一个循环:

function getNextSibling(node, type){
    while((node = node.nextSibling) && (node.nodeName != type));
    return node;
}

getNextSibling(elem, 'SPAN');

有Element.nextElementSibling,在IE9 +,Fx3.5 +,Op,Saf和Chrome(最近三个版本中都是Dunno版本,但有相当长的一段时间)中受支持。

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

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