Js allows you to insert comments into emoticon images and insert characters at the cursor.

Source: Internet
Author: User

This article introduces how to insert characters in comments, emotices, and images at the cursor. For more information, see references.

Each comment added to the database represents a special character. For example, "[face1]" represents a smiling face, and "[face2]" represents a sad face. However, before the comment is displayed, replace all the special characters in the comment with the corresponding image.

The Code is as follows: Copy code


<? Php
/**
* Add emoticons to comments-insert emoticons (www. bKjia. c0m) at the cursor)
*/
$ Content = isset ($ _ POST ["content"])? $ _ POST ["content"]: '';
If (! Empty ($ content ))
{
For ($ I = 1; $ I <= 13; $ I ++)
{
$ Content = str_replace ('[face '. $ I. ']', ' ', $ content); // Replace special characters with images for display (you can also replace regular expressions, but it is said that regular expressions are less efficient)
}
Echo 'the content you entered is: '. $ content.' <br/> ';
}
 
?>
<Script type = "text/javascript">
// Insert the image character JS at the cursor
Function addExpression (myField, myValue)
{
MyField = document. getElementById (myField );
// IE
If (document. selection)
{
MyField. focus ();
Sel = document. selection. createRange ();
MyValue = "[face" + myValue + "]";
Sel. text = myValue;
Sel. select ();
}
// MOZILLA
Else if (myField. selectionStart | myField. selectionStart = '0 ')
{
Var startPos = myField. selectionStart;
Var endPos = myField. selectionEnd;
// Save scrollTop before insert
Var restoreTop = myField. scrollTop;
MyValue = "[face" + myValue + "]";
MyField. value = myField. value. substring (0, startPos) + myValue + myField. value. substring (endPos, myField. value. length );
If (restoreTop> 0)
{
// Restore previous scrollTop
MyField. scrollTop = restoreTop;
}
MyField. focus ();
MyField. selectionStart = startPos + myValue. length;
MyField. selectionEnd = startPos + myValue. length;
}
Else
{
MyField. value + = myValue;
MyField. focus ();
}
}
 
</Script>
 
<Br/> <p/>













<Br/>
<Form action = "" method = "post">
<Textarea name = "content" id = "content"> </textarea>
<Input type = "submit" value = "submit"/>
</Form>

Related Article

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.