|
以下是我个人从网上摘录的或自写的javasript函数,就当抛砖引玉吧:
一、js要经常获取或设置url地址中的参数部分,故我写了如下2个函数: //获取URL地址的QuestString //参数1:url表示传入的地址,多为window.location.href.search 或 document.URL //参数2:questname表示参数名,比如http://www.google.cn?q=js中的字符q function GetQuestValue(url,questname) {var v='';var q=url.replace(/.*\?/,"").split("&"); var forsearch=new RegExp(questname+'=',"i"); for(var i=0;i<q.length;i++) { if(q[i].search(forsearch)>=0) {v=q[i].replace(forsearch,"")} } return v; } //根据传入的URL地址重新更新QuestString,建议questValue若为中文,用encodeURIComponent()转换下 //返回值为更改过的url地址 function SetQuestUrl(url,questname,questValue) { var s=unescape(url);var q=questname + "=" + questValue; if(s.search(/\?/)>=0) {var forsearch=new RegExp(questname+'=\.*&?',"i");var x_forsearch=new RegExp(questname+'=\.*&',"i"); if(s.search(forsearch)>=0){if(s.search(x_forsearch)>=0){s=s.replace(x_forsearch,q+'&')} else{s=s.replace(forsearch,q)}} else{s=s+'&'+q}} else{s=s+'?'+q;} return s; } 二、在。NET中,如何用js获取RadioButtonList、CheckBoxList的选中值 //返回RadioButtonList选中的value, //.net2.0通用,由于考虑到横排和竖排的情况,以及设置了RepeatColumns情况下,遍历cell的个数为佳 //传入的id为RadioButtonList的id function rblValue(id) { var r=document.getElementById(id) var l=0; for(var c=0;c<r.rows.length;c++) {l=l+r.rows[c].cells.length} //alert(l); for(var i=0;i<l;i++) { var rn=id+"_"+i; if(document.getElementById(rn).checked) { return document.getElementById(rn).value } } return '' } //返回CheckBoxList选中的Text //关键在于获取该CheckBox后一个DOM,后一个DOM就是其关联的label,在C#里设置的Text也显示在label里 //传入的id为RadioButtonList的id function cklLabelfor(id) { var r=document.getElementById(id) var l=0;var st=""; for(var c=0;c<r.rows.length;c++) {l=l+r.rows[c].cells.length} for(var i=0;i<l;i++) { var rn=id+"_"+i; if(document.getElementById(rn).checked) {st+=document.getElementById(rn).nextSibling.innerHTML+"\n";} } return st; } 三、js判断鼠标一般用onmouseover和onmouseout来触发,但如果目标元素包含子元素,当鼠标移动到子元素也会触发该onmouseout和onmouseover,故从网上摘录如下3个函数(在FF下有点小BUG): //遍历判断current是否为container的子元素 function containsDOM (container, current) {var isParent = false; while (current != null && !isParent) {isParent = (container == current); current = current.parentNode;} return isParent;} //判断鼠标是否进入该元素(包括子元素) function IsonMouseEnter(element,evt) {var obj=null; if(evt.fromElement){obj=evt.fromElement} else if(evt.relatedTarget){obj=evt.relatedTarget;} return !containsDOM(element,obj) } //判断鼠标是否离开该元素(包括子元素) function IsonMouseLeave(element,evt) {var obj=null; if(evt.toElement){obj=evt.toElement} else if(evt.relatedTarget){obj=evt.relatedTarget;} return !containsDOM(element,obj) } 应用如下: <div onmouseout="if(IsonMouseLeave(this,event)){//这里添加js代码}" onmouseover="if(IsonMouseEnter(this,event)){//这里添加js代码}"> 四、判断传入的字符串实际长度,一般中文占2个字符: function truewordlength(st) {var l=st.length;var china=0;var english=0; for(var i=0;i<l;i++){if(st.charCodeAt(i)>255){china+=2;}else{english+=1;}} return china+english; } 五、判断客户端浏览器: function CheckNavigatorMes() { if((navigator.userAgent.indexOf('MSIE') >= 0) && (navigator.userAgent.indexOf('Opera') < 0)) {return "MSIE"} if(navigator.userAgent.indexOf('Firefox') >= 0) {return "Firefox"} if(navigator.userAgent.indexOf('Opera') >= 0) {return "Opera"} return "" } 六、将字符串赋给textarea输入框的选中文本,如未选中则在末尾插入: //支持FF、IE //先要有个全局变量:pos var pos=null; //该方法将获取选中文本对象赋给pos function getPosition(obj) { if(document.selection) { obj.focus();//IE非常喜欢自己发挥。。。 pos=document.selection.createRange(); //obj.blur(); //alert(pos.text); } else if(obj.selectionStart||obj.selectionStart=='0')// { //alert(typeof(obj.selectionStart)); pos={"start":obj.selectionStart,"end":obj.selectionEnd}; } else pos=null; } //将字符串v插入<textarea>obj对象中 function setPosition(v,obj) { //if(pos==null)pos=getPosition(obj) if(pos!=null) { if(document.selection) { //obj.focus(); pos.text=v; //alert(pos+'--'+pos.text) } else{obj.value=obj.value.substring(0,pos.start)+v+obj.value.substring(pos.end,obj.value.length)} } else{obj.value+=v;}//alert(obj.value) } 应用如下: <textarea onclick="getPosition(this)" onselect="getPosition(this)" onchange="getPosition(this)"></textarea> |
发表于 @ 2009年03月12日 15:47:00 | 评论( loading... ) | 举报| 收藏