1,打上checkbox,实现对文本框中内容的格式化
Code
function Chkbacksend()
{
var befvalue=document.getElementById("txtContent").value;
if(document.getElementById("chkMustBack").checked)
{
document.getElementById("txtContent").value=befvalue.EndFlag().AddTip(true);
}
else
{
document.getElementById("txtContent").value=befvalue.EndFlag().AddTip(false);
}
}
2,prototype必须以标点结尾
Code
String.prototype.EndFlag=function()
{
var pattern = /(\.|。|!|!|\?|?|,|,)$/;
if(this.length>0)
{
if(pattern.test(this))
{
return this
}
else
{
return this+"。";
}
}
return this;
}
3,prototype添加特定的字符串
Code
String.prototype.AddTip = function(flag)
{
var newcontents=this;
if(flag)
{
if(!(this.indexOf("请回复!")>-1))
{
newcontents=this+"请回复!";
}
}
else
{
if(this.indexOf("请回复!")>-1)
{
newcontents=this.replace("请回复!","");
}
}
return newcontents;
}
我的网摘中存放了个精彩的脚本代码优化的文章.借鉴的还很不到位!