對TEXTAREA的添加內容的JS功能代碼 http://www.powerpoint-group.com/xiaoyou/ly/bookadd.asp类似上面网站的留言表情功能。网上看了一些类似的功能代码,不怎么么明白会JS的高手帮忙写写。到底是怎么实现添加内容的。先谢了! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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]');" 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"/>这样的图片表情,那图片就能显示了 请教个正则。在线等。谢谢。 js中的地址 eval是不是不能返回对象 急-IE状态栏的问题 下拉菜单的问题 研究课题!!!!请进! 如何通过JS来控制文本框中输入的为数字而且为1-100 发个自己写的脚本程序看看JScript.NET的。 关于如何在Javascript中实现asp对数据库进行操作? 谁有javascript 树型菜单源代码? 急用 服务器 如何传回 HTML 快照 关于js套嵌问题,希望大家指点。谢谢·!
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]');"
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"/>这样的图片表情,
那图片就能显示了