简体   繁体   中英

Ckeditor getSelectedElement always null

I am trying to get the selected element in Ckeditor. Ie if the html is:

This has <a href="www.google.com">a link</a>.

I want to retrieve the entire a element when I select the link text.

As I understand it the following code:

editor.getSelection().getSelectedElement()

is supposed to do that. But it is always returning null (no matter how much or how little I select). I have tried various other element types and the result is always the same.

getSelection() by itself is not returning null. I am able to get just the link text with

editor.getSelection().getNative().toString().

I even tried some old code that I found here:

http://cksource.com/forums/viewtopic.php?f=6&t=11997&p=31833

but that resulted in the error Object #<Object> has no method 'getRangeAt'

Does anyone have any suggestions?

I was too troubleshooting the same thing. But got this code working. Just check if it helps

var element = CKEDITOR.plugins.link.getSelectedLink( editor );
    if ( element )
    {
       if ( element.is( 'a' ) )
       {
          var urldata = element.getAttribute( 'href' );
          this.setValue(urldata);
        }
       }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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