A simpler iframe inserts a picture from the cursor (it can still be inserted in the original position after losing focus)

Source: Internet
Author: User

Reprint Please specify: theviper http://www.cnblogs.com/TheViper

The night before yesterday found an IFRAME from the cursor Insert picture (after losing focus can still insert inside the original location of the usage in the ie6,7 invalid, good sad reminder, originally only tested IE8 thought in ie6,7 also no problem.

I found a good rich text editor on GitHub yesterday Wangeditor, a look at the name is written by the Chinese. The editor is good at supporting ie6+, and the most important point is that it can still insert pictures in the original position after losing focus on ie6,7,8, and the amount of code is very small. So curious to see how it was done, cut it out, and that's all

1 varCurrentrange,_parentelem,supportrange =typeofDocument.createrange = = = ' function ';2     functionGetcurrentrange () {3         varselection,4 Range,5TXT = $ (' editor ');6         if(supportrange) {7Selection =document.getselection ();8             if(Selection.getrangeat &&selection.rangecount) {9Range = Document.getselection (). Getrangeat (0);Ten_parentelem =Range.commonancestorcontainer; One             } A}Else{ -Range =Document.selection.createRange (); -_parentelem =range.parentelement (); the         } -         if(_parentelem && (avalon.contains (TXT, _parentelem) | | txt = = =_parentelem)) { -Parentelem =_parentelem; -             returnrange; +         } -         returnrange; +     } A     functionsaveselection () { atCurrentrange =Getcurrentrange (); -     } -     functionrestoreselection () { -         if(!Currentrange) { -             return; -         } in         varselection, - range; to         if(supportrange) { +Selection =document.getselection (); - selection.removeallranges (); the Selection.addrange (currentrange); *}Else{ $Range =Document.selection.createRange ();Panax NotoginsengRange.setendpoint (' Endtoend ', currentrange); -             if(CurrentRange.text.length = = 0){ theRange.collapse (false); +}Else{ ARange.setendpoint (' Starttostart ', currentrange); the             } + Range.Select (); -         } $}

This is much less than the one in the last article that was stripped from the kindeditor, and seems to be at a glance.

How to use it?

1     functioninsertimage (HTML) {2 restoreselection ();3         if(document.selection)4 currentrange.pastehtml (HTML);5         Else6Document.execcommand ("Insertimage",false, HTML);7 saveselection ();8     }9Avalon.bind ($ (' post_input '), ' MouseUp ',function(e) {Ten saveselection (); One     }); AAvalon.bind ($ (' post_input '), ' KeyUp ',function(e) { - saveselection (); -});

As in the previous article, the editor's Div must be keyup,mouseup bound to save the selection,range so that you can still insert the picture in the original position after losing focus. Call the time directly insertimage (HTML) is ready. This is not an IFRAME, it's a div contenteditable=true.

Wangeditor inside the example is to insert the outer chain image, only one image can be inserted at a time. Wangeditor source for unified use is Document.execcommand ("Insertimage", false,html);. However, there is a problem with this method, that is, in ie6,7,8, if you want to insert multiple images, only a single image will be inserted in the original position.

First comment out the IF

Insert two pictures at a time

This is a strict point, IE6.

Ie7

Ie8

The solution is if it is ie6,7,8, currentrange.pastehtml (HTML);. Insert HTML, that is, remove the if comment above. Of course the inserted is no longer the image address, now is the entire IMG tag containing the image address

Ket

Ie7

Ie8

Finally attached example download

A simpler iframe inserts a picture from the cursor (it can still be inserted in the original position after losing focus)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.