Native JS-written emoji expression selector

Source: Internet
Author: User

I write this thing to find N expression selector, all use jquery, mobile is not suitable, because jquery with min all have to 90k. And does not need the expression to turn the text or the text to the expression this function.

This article uses the div input box, and selects the expression to insert a piece of HTML directly into the text content (emoji is the SVG format), the effect is visible.

On the Code

Html

<name= "Note"  class= "Newpost"  ID= "Emojiinput"  contenteditable= "true"></div><  ID= "Face"></div>

Css

. Emojisvg{width:2em;Height:2em; }Div.newpost{text-align:Center;Line-height:18px;font-size:16px;padding:0px;background:#fff;width:100%;Height:100px;_height:16px;Margin-left:Auto;Margin-right:Auto;Margin-bottom:15px;Outline:0;Word-wrap:Break-word;overflow-y:Hidden;Z-index: +;Border-bottom:1px solid #cccccc; }#face{padding:5px; }

JS (if there are other onload,ready in the page, please put this code in

//Generate EmoticonsWindow.onload =function() {                varface = document.getElementById (' Face ');  for(vari = 0; I < 38; i++) {                    varA = Document.createelement ("a"); A.href= "javascript:;"; if(I < 10) {a.innerhtml= ' ; } Else{a.innerhtml= ' ;                } face.appendchild (a);            }; }            //Click Insert ExpressionSetTimeout (function() {                varPickers = Face.getelementsbytagname (' A '); varEmojiinput = document.getElementById (' emojiinput '));  for(vari = 0; i < pickers.length; i++) {Pickers[i].onclick=function(e) {document.getElementById (' Emojiinput '). focus ();Inserthtmlatcaret ( This. InnerHTML); //emojiinput.innerhtml+=this.innerhtml;                    }                }                functionInserthtmlatcaret (HTML) {varsel, Range; if(window.getselection) {//IE9 and Non-ieSEL =window.getselection (); if(Sel.getrangeat &&sel.rangecount) {range= Sel.getrangeat (0);                            Range.deletecontents (); //range.createcontextualfragment () would be useful                            //non-standard and not supported in all browsers (IE9, for one)                            varel = document.createelement ("div"); El.innerhtml=html; varFrag =document.createdocumentfragment (), node, lastnode;  while(node =el.firstchild)) {Lastnode=frag.appendchild (node);                            } range.insertnode (Frag); //Preserve the selection                            if(lastnode) {range=Range.clonerange ();                                Range.setstartafter (Lastnode); Range.collapse (true);                                Sel.removeallranges ();                            Sel.addrange (range); }                        }                    } Else if(document.selection && Document.selection.type! = "Control") {                        //IE < 9Document.selection.createRange (). pastehtml (HTML); }                }            }, 500);

Native JS-written emoji expression selector

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.