JS implementation comment Insert emoticons at cursor Insert character

Source: Internet
Author: User

Each expression in each comment added to the database is expressed in a special character, such as "[Face1]" represents a smiley face, "[Face2]" represents a sad expression. Just, when this comment is displayed, the special characters of all the expressions in the comments are replaced with the corresponding pictures.

The code is as follows Copy Code


<?php
/**
* Comment Add expression--insert emoticons at cursor (www.111cn.net)
*/
$content = isset ($_post["content")? $_post["Content"]: ';
if (!empty ($content))
{
for ($i = 1; $i <= $i + +)
{
$content = Str_replace (' face '. $i. '] ', ' ', $content); Replace special characters with picture display (also available as regular substitutions, but are said to be inefficient)
}
Echo ' The content you entered is: '. $content. ' <br/> ';
}

?>
<script type= "Text/javascript" >
Inserts a picture 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= "submitted"/>
</form>

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.