javascript常用代码(去字串两端空格、文本域光标定位到最后)
//去字串两端空白
function trim(str){
if(null==str) return null;
var tmp = str.replace(/(^\\s\*)|(\\s\*$)/g, "");
return tmp;
}
//去字串所有空白
function trimAll(str){
if(null==str) return null;
var tmp = str.replace(/\\s\*/g, "");
return tmp;
}
alert(“[“+opMap.trim(“ 23 ssew \n\t”)+”]“);
alert(“[“+opMap.trimAll(“ 23 ssew \n\t”)+”]“);
或
String.prototype.trim=function(){
return this.replace(/(^\s*)|(\s*$)/g, “”);
};
String.prototype.trimAll=function(){
return this.replace(/\s*/g, “”);
};
alert(“[“+” 23 ssew \n\t”.trim()+”]“);
alert(“[“+” 23 ssew \n\t”.trimAll()+”]“);
//文本域光标定位到最后
function moveTextAreaFocusToLast(){
//文本域对象
var obj=document.getElementById("txtAreaContent");
obj.focus();
var len = obj.value.length;
if (document.selection) {
var sel = obj.createTextRange();
sel.moveStart(‘character’,len);
sel.collapse();
sel.select();
} else if (typeof obj.selectionStart == ‘number’ && typeof obj.selectionEnd == ‘number’) {
obj.selectionStart = obj.selectionEnd = len;
}
}
参考//www.cainiao8.com/web/js\_note/js\_regular\_expression.html
还没有评论,来说两句吧...