http://www.powerpoint-group.com/xiaoyou/ly/bookadd.asp类似上面网站的留言表情功能。
网上看了一些类似的功能代码,不怎么么明白
会JS的高手帮忙写写。到底是怎么实现添加内容的。先谢了!

解决方案 »

  1.   


    function grin(tagname,tag) {

    alert("tagname ="+tagname);
            var myField;
            if (document.getElementById(tagname) && document.getElementById(tagname).type == 'textarea') {
                    myField = document.getElementById(tagname);
            }
            else {
                    return false;
            }
            if (document.selection) {
                    myField.focus();
                    sel = document.selection.createRange();
                    sel.text = tag;
                    myField.focus();
            }
            else if (myField.selectionStart || myField.selectionStart == '0') {
                    var startPos = myField.selectionStart;
                    var endPos = myField.selectionEnd;
                    var cursorPos = endPos;
                    myField.value = myField.value.substring(0, startPos)
                                              + tag
                                              + myField.value.substring(endPos, myField.value.length);
                    cursorPos += tag.length;
                    myField.focus();
                    myField.selectionStart = cursorPos;
                    myField.selectionEnd = cursorPos;
            }
            else {
                    myField.value += tag;
                    myField.focus();
            }
    }这个是上面网站的JS代码调用方法 onClick="grin('b_conter','[Q20]');" 
      

  2.   


    function grin(tagname,tag) {
            
        alert("tagname ="+tagname);
            var myField;
            if (document.getElementById(tagname) && document.getElementById(tagname).type == 'textarea') {
                    myField = document.getElementById(tagname);//通过传入的id获得元素,没有找到则返回false
            }
            else {
                    return false;
            }
            if (document.selection) {
                    myField.focus();
                    sel = document.selection.createRange();//或去选中的内容,然后替换成tag也就是[Q20]
                    sel.text = tag;
                    myField.focus();
            }
            else if (myField.selectionStart || myField.selectionStart == '0') {
                    var startPos = myField.selectionStart;
                    var endPos = myField.selectionEnd;
                    var cursorPos = endPos;
                    myField.value = myField.value.substring(0, startPos)
                                              + tag
                                              + myField.value.substring(endPos, myField.value.length);
                    cursorPos += tag.length;//这里是兼容ff其实也是一样替换内容表情
                    myField.focus();
                    myField.selectionStart = cursorPos;
                    myField.selectionEnd = cursorPos;
            }
            else {
                    myField.value += tag;
                    myField.focus();
            }
    }
    主要原理是将内容替换成[Q20],然后会对[Q20]这种字符进行正则替换成<img src="Q20.jsp"/>这样的图片表情,
    那图片就能显示了