js

来源:互联网 发布:产业并购基金 知乎 编辑:程序博客网 时间:2024/04/28 17:39

js请求地址中包含中文时解决乱码:encodeURIComponent();

关闭ajax异步请求:$.ajaxSettings.async = false;

checkboc选择器在制定上下文中选择:$('input:checkbox[name="multiple"]:checked',$('#conttextId'))和$("input[name='multiple']:checked",$('#conttextId'))是一样的




//删除字符串的子串
function removeStrWithcomma(srcStr,targetStr){
   var returnStr = '';
   if (srcStr == null || srcStr == '' || srcStr == undefined) {
       return returnStr;
   }
   else{    
    if (srcStr.indexOf(targetStr) == -1) {
    return srcStr;
    }
    else {
    var srcStrs = srcStr.split(',');
    if (srcStrs.length == 1) {
    returnStr = '';
    }
    else
    if (srcStr.match(eval("/^" + targetStr + ".*/")) != null) {
    returnStr = srcStr.replace(targetStr + ',', '');
    }
    else
    if (srcStr.match(eval("/.*" + targetStr + "$/")) != null) {
    returnStr = srcStr.replace(',' + targetStr, '');
    }
    else {
    returnStr = srcStr.replace(',' + targetStr, '');
    }
    }
    return returnStr;
   }
}
    //字符串去重
function filterRepeatStr(str){ 
var ar2 = str.split(","); 
var array = new Array(); 
var j=0 
for(var i=0;i<ar2.length;i++){ 
if((array == "" || array.toString().match(new RegExp(ar2[i],"g")) == null)&&ar2[i]!=""){ 
array[j] =ar2[i]; 
//array.sort(); 
j++; 


return array.toString(); 
}



//去除js空格

String.prototype.Trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}





//

var array = [    {name: 'a', phone: 1, value: 'val_4'},    {name: 'b', phone: 5, value: 'val_3'},    {name: 'd', phone: 3, value: 'val_2'},    {name: 'c', phone: 4, value: 'val_1'}]array.sort(getSortFun('desc', 'phone'));function getSortFun(order, sortBy) {    var ordAlpah = (order == 'asc') ? '>' : '<';    var sortFun = new Function('a', 'b', 'return a.' + sortBy + ordAlpah + 'b.' + sortBy + '?1:-1');    return sortFun;}alert(JSON.stringify(array));
</pre><pre name="code" class="js:nocontrols" style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 13px; line-height: 19.5px; background-color: rgb(255, 255, 255);">----------------------------------------------------------------------------------------------------
解决日期小例子
<script type="text/javascript">var minutes = 1000 * 60var hours = minutes * 60var days = hours * 24var years = days * 365var t = Date.parse("Jul 8, 2005")var y = t/yearsdocument.write("It's been: " + y + " years from 1970/01/01")document.write(" to 2005/07/08!");var date1 = Date.parse("2014-07-10");var date2 = Date.parse("2014-07-25");alert(date1<date2);alert(date1+"--------------"+date2);alert((date2-date1)/days<15);var mycars=new Array();mycars.push(1);mycars.push(2);mycars.push(3);mycars.push(1);mycars.push(1);mycars=$.unique(mycars);alert(mycars);</script>
</pre><pre name="code" class="js:nocontrols" style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 13px; line-height: 19.5px; background-color: rgb(255, 255, 255);">-----------------------------------------------------------------------------------------------------------
jquery小特效,3秒钟后消失 $("#returnMsg").show(300).delay(5000).hide(300);
------------------------------------------------------------------------------------------------------------
  <title>表单验证类 Validator v1.05</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <style> body,td{font:normal 12px Verdana;color:#333333} input,textarea,select,td{font:normal 12px Verdana;color:#333333;border:1px solid #999999;background:#ffffff} table{border-collapse:collapse;} td{padding:3px} input{height:20;} textarea{width:80%;height:50px;overflow:auto;} form{display:inline} </style> <table align="center">  <form name="theForm" id="demo" method="get" onSubmit="return Validator.Validate(this,2)">  <tr>   <td>身份证号:</td><td><input name="Card" dataType="IdCard" msg="身份证号错误"></td>  </tr>    <tr>   <td>真实姓名:</td><td><input name="Name" dataType="Chinese" msg="真实姓名只允许中文"></td>  </tr>  <tr>   <td>ID:</td><td><input name="username" dataType="Username" msg="ID名不符合规定"></td>  </tr>  <tr>   <td>英文名:</td><td><input name="Nick" dataType="English" require="false" msg="英文名只允许英文字母"></td>  </tr>    <tr>   <td>主页:</td><td><input name="Homepage" require="false" dataType="Url"   msg="非法的Url"></td>  </tr>  <tr>   <td>密码:</td><td><input name="Password" dataType="SafeString"   msg="密码不符合安全规则" type="password"></td>  </tr>  <tr>   <td>重复:</td><td><input name="Repeat" dataType="Repeat" to="Password" msg="两次输入的密码不一致" type="password"></td>  </tr>  <tr>   <td>信箱:</td><td><input name="Email" dataType="Email" msg="信箱格式不正确"></td>  </tr>    <tr>   <td>信箱:</td><td><input name="Email" dataType="Repeat" to="Email" msg="两次输入的信箱不一致"></td>  </tr>  <tr>   <td>QQ:</td><td><input name="QQ" require="false" dataType="QQ" msg="QQ号码不存在"></td>  </tr>    <tr>   <td>身份证:</td><td><input name="Card" dataType="IdCard" msg="身份证号码不正确"></td>  </tr>  <tr>   <td>年龄:</td><td><input name="Year" dataType="Range" msg="年龄必须在18~28之间" min="18" max="28"></td>  </tr>   <tr>   <td>年龄1:</td><td><input name="Year1" require="false" dataType="Compare" msg="年龄必须在18以上" to="18" operator="GreaterThanEqual"></td>  </tr>   <tr>   <td>电话:</td><td><input name="Phone" require="false" dataType="Phone" msg="电话号码不正确"></td>  </tr>   <tr>   <td>手机:</td><td><input name="Mobile" require="false" dataType="Mobile" msg="手机号码不正确"></td>  </tr>     <tr>   <td>生日:</td><td><input name="Birthday" dataType="Date" format="ymd" msg="生日日期不存在"></td>  </tr>   <tr>   <td>邮政编码:</td><td><input name="Zip" dataType="Custom" regexp="^[1-9]\d{5}$" msg="邮政编码不存在"></td>  </tr>  <tr>   <td>邮政编码:</td><td><input name="Zip1" dataType="Zip" msg="邮政编码不存在"></td>  </tr>  <tr>   <td>操作系统:</td><td><select name="Operation" dataType="Require"  msg="未选择所用操作系统" ><option value="">选择您所用的操作系统</option><option value="Win98">Win98</option><option value="Win2k">Win2k</option><option value="WinXP">WinXP</option></select></td>  </tr>  <tr>   <td>所在省份:</td><td>广东<input name="Province" value="1" type="radio">陕西<input name="Province" value="2" type="radio">浙江<input name="Province" value="3" type="radio">江西<input name="Province" value="4" type="radio" dataType="Group"  msg="必须选定一个省份" ></td>  </tr>  <tr>   <td>爱好:</td><td>运动<input name="Favorite" value="1" type="checkbox">上网<input name="Favorite" value="2" type="checkbox">听音乐<input name="Favorite" value="3" type="checkbox">看书<input name="Favorite" value="4" type="checkbox"" dataType="Group" min="2" max="3"  msg="必须选择2~3种爱好"></td>  </tr>   <td>自我介绍:</td><td><textarea name="Description" dataType="Limit" max="10"  msg="自我介绍内容必须在10个字之内">中文是一个字</textarea></td>  </tr>     <td>自传:</td><td><textarea name="History" dataType="LimitB" min="3" max="10"  msg="自传内容必须在[3,10]个字节之内">中文是两个字节t</textarea></td>  </tr>    <tr>   <td>相片上传:</td><td><input name="up" dataType="Filter" msg="非法的文件格式" type="file" accept="jpg, gif, png"></td>  </tr>  <tr>   <td colspan="2"><input name="Submit" type="submit" value="确定提交"><input onClick="Validator.Validate(document.getElementById('demo'))" value="检验模式1" type="button"><input onClick="Validator.Validate(document.getElementById('demo'),2)" value="检验模式2" type="button"><input onClick="Validator.Validate(document.getElementById('demo'),3)" value="检验模式3" type="button"></td>  </tr>  </form> </table><script>  /*************************************************<span style="white-space:pre"></span>Validator v1.05<span style="white-space:pre"></span>code by 我佛山人<span style="white-space:pre"></span>wfsr@msn.com*************************************************/ Validator = {<span style="white-space:pre"></span>Require : /.+/,<span style="white-space:pre"></span>Email : /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,<span style="white-space:pre"></span>Phone : /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/,<span style="white-space:pre"></span>Mobile : /^((\(\d{2,3}\))|(\d{3}\-))?13\d{9}$/,<span style="white-space:pre"></span>Url : /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/,<span style="white-space:pre"></span>IdCard : "this.IsIdCard(value)",<span style="white-space:pre"></span>Currency : /^\d+(\.\d+)?$/,<span style="white-space:pre"></span>Number : /^\d+$/,<span style="white-space:pre"></span>Zip : /^[1-9]\d{5}$/,<span style="white-space:pre"></span>QQ : /^[1-9]\d{4,8}$/,<span style="white-space:pre"></span>Integer : /^[-\+]?\d+$/,<span style="white-space:pre"></span>Double : /^[-\+]?\d+(\.\d+)?$/,<span style="white-space:pre"></span>English : /^[A-Za-z]+$/,<span style="white-space:pre"></span>Chinese :  /^[\u0391-\uFFE5]+$/,<span style="white-space:pre"></span>Username : /^[a-z]\w{3,}$/i,<span style="white-space:pre"></span>UnSafe : /^(([A-Z]*|[a-z]*|\d*|[-_\~!@#\$%\^&\*\.\(\)\[\]\{\}<>\?\\\/\'\"]*)|.{0,5})$|\s/,<span style="white-space:pre"></span>IsSafe : function(str){return !this.UnSafe.test(str);},<span style="white-space:pre"></span>SafeString : "this.IsSafe(value)",<span style="white-space:pre"></span>Filter : "this.DoFilter(value, getAttribute('accept'))",<span style="white-space:pre"></span>Limit : "this.limit(value.length,getAttribute('min'),  getAttribute('max'))",<span style="white-space:pre"></span>LimitB : "this.limit(this.LenB(value), getAttribute('min'), getAttribute('max'))",<span style="white-space:pre"></span>Date : "this.IsDate(value, getAttribute('min'), getAttribute('format'))",<span style="white-space:pre"></span>Repeat : "value == document.getElementsByName(getAttribute('to'))[0].value",<span style="white-space:pre"></span>Range : "getAttribute('min') < (value|0) && (value|0) < getAttribute('max')",<span style="white-space:pre"></span>Compare : "this.compare(value,getAttribute('operator'),getAttribute('to'))",<span style="white-space:pre"></span>Custom : "this.Exec(value, getAttribute('regexp'))",<span style="white-space:pre"></span>Group : "this.MustChecked(getAttribute('name'), getAttribute('min'), getAttribute('max'))",<span style="white-space:pre"></span>ErrorItem : [document.forms[0]],<span style="white-space:pre"></span>ErrorMessage : ["以下原因导致提交失败:\t\t\t\t"],<span style="white-space:pre"></span>Validate : function(theForm, mode){<span style="white-space:pre"></span>var obj = theForm || event.srcElement;<span style="white-space:pre"></span>var count = obj.elements.length;<span style="white-space:pre"></span>this.ErrorMessage.length = 1;<span style="white-space:pre"></span>this.ErrorItem.length = 1;<span style="white-space:pre"></span>this.ErrorItem[0] = obj;<span style="white-space:pre"></span>for(var i=0;i<count;i++){<span style="white-space:pre"></span>with(obj.elements[i]){<span style="white-space:pre"></span>var _dataType = getAttribute("dataType");<span style="white-space:pre"></span>if(typeof(_dataType) == "object" || typeof(this[_dataType]) == "undefined")  continue;<span style="white-space:pre"></span>this.ClearState(obj.elements[i]);<span style="white-space:pre"></span>if(getAttribute("require") == "false" && value == "") continue;<span style="white-space:pre"></span>switch(_dataType){<span style="white-space:pre"></span>case "IdCard" :<span style="white-space:pre"></span>case "Date" :<span style="white-space:pre"></span>case "Repeat" :<span style="white-space:pre"></span>case "Range" :<span style="white-space:pre"></span>case "Compare" :<span style="white-space:pre"></span>case "Custom" :<span style="white-space:pre"></span>case "Group" : <span style="white-space:pre"></span>case "Limit" :<span style="white-space:pre"></span>case "LimitB" :<span style="white-space:pre"></span>case "SafeString" :<span style="white-space:pre"></span>case "Filter" :<span style="white-space:pre"></span>if(!eval(this[_dataType]))<span style="white-space:pre"></span>{<span style="white-space:pre"></span>this.AddError(i, getAttribute("msg"));<span style="white-space:pre"></span>}<span style="white-space:pre"></span>break;<span style="white-space:pre"></span>default :<span style="white-space:pre"></span>if(!this[_dataType].test(value)){<span style="white-space:pre"></span>this.AddError(i, getAttribute("msg"));<span style="white-space:pre"></span>}<span style="white-space:pre"></span>break;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}<span style="white-space:pre"></span>if(this.ErrorMessage.length > 1){<span style="white-space:pre"></span>mode = mode || 1;<span style="white-space:pre"></span>var errCount = this.ErrorItem.length;<span style="white-space:pre"></span>switch(mode){<span style="white-space:pre"></span>case 2 :<span style="white-space:pre"></span>for(var i=1;i<errCount;i++)<span style="white-space:pre"></span>this.ErrorItem[i].style.color = "red";<span style="white-space:pre"></span>case 1 :<span style="white-space:pre"></span>alert(this.ErrorMessage.join("\n"));<span style="white-space:pre"></span>this.ErrorItem[1].focus();<span style="white-space:pre"></span>break;<span style="white-space:pre"></span>case 3 :<span style="white-space:pre"></span>for(var i=1;i<errCount;i++){<span style="white-space:pre"></span>try{<span style="white-space:pre"></span>var span = document.createElement("SPAN");<span style="white-space:pre"></span>span.id = "__ErrorMessagePanel";<span style="white-space:pre"></span>span.style.color = "red";<span style="white-space:pre"></span>this.ErrorItem[i].parentNode.appendChild(span);<span style="white-space:pre"></span>span.innerHTML = this.ErrorMessage[i].replace(/\d+:/,"*");<span style="white-space:pre"></span>}<span style="white-space:pre"></span>catch(e){alert(e.description);}<span style="white-space:pre"></span>}<span style="white-space:pre"></span>this.ErrorItem[1].focus();<span style="white-space:pre"></span>break;<span style="white-space:pre"></span>default :<span style="white-space:pre"></span>alert(this.ErrorMessage.join("\n"));<span style="white-space:pre"></span>break;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>return false;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>return true;<span style="white-space:pre"></span>},<span style="white-space:pre"></span>limit : function(len,min, max){<span style="white-space:pre"></span>min = min || 0;<span style="white-space:pre"></span>max = max || Number.MAX_VALUE;<span style="white-space:pre"></span>return min <= len && len <= max;<span style="white-space:pre"></span>},<span style="white-space:pre"></span>LenB : function(str){<span style="white-space:pre"></span>return str.replace(/[^\x00-\xff]/g,"**").length;<span style="white-space:pre"></span>},<span style="white-space:pre"></span>ClearState : function(elem){<span style="white-space:pre"></span>with(elem){<span style="white-space:pre"></span>if(style.color == "red")<span style="white-space:pre"></span>style.color = "";<span style="white-space:pre"></span>var lastNode = parentNode.childNodes[parentNode.childNodes.length-1];<span style="white-space:pre"></span>if(lastNode.id == "__ErrorMessagePanel")<span style="white-space:pre"></span>parentNode.removeChild(lastNode);<span style="white-space:pre"></span>}<span style="white-space:pre"></span>},<span style="white-space:pre"></span>AddError : function(index, str){<span style="white-space:pre"></span>this.ErrorItem[this.ErrorItem.length] = this.ErrorItem[0].elements[index];<span style="white-space:pre"></span>this.ErrorMessage[this.ErrorMessage.length] = this.ErrorMessage.length + ":" + str;<span style="white-space:pre"></span>},<span style="white-space:pre"></span>Exec : function(op, reg){<span style="white-space:pre"></span>return new RegExp(reg,"g").test(op);<span style="white-space:pre"></span>},<span style="white-space:pre"></span>compare : function(op1,operator,op2){<span style="white-space:pre"></span>switch (operator) {<span style="white-space:pre"></span>case "NotEqual":<span style="white-space:pre"></span>return (op1 != op2);<span style="white-space:pre"></span>case "GreaterThan":<span style="white-space:pre"></span>return (op1 > op2);<span style="white-space:pre"></span>case "GreaterThanEqual":<span style="white-space:pre"></span>return (op1 >= op2);<span style="white-space:pre"></span>case "LessThan":<span style="white-space:pre"></span>return (op1 < op2);<span style="white-space:pre"></span>case "LessThanEqual":<span style="white-space:pre"></span>return (op1 <= op2);<span style="white-space:pre"></span>default:<span style="white-space:pre"></span>return (op1 == op2);            <span style="white-space:pre"></span>}<span style="white-space:pre"></span>},<span style="white-space:pre"></span>MustChecked : function(name, min, max){<span style="white-space:pre"></span>var groups = document.getElementsByName(name);<span style="white-space:pre"></span>var hasChecked = 0;<span style="white-space:pre"></span>min = min || 1;<span style="white-space:pre"></span>max = max || groups.length;<span style="white-space:pre"></span>for(var i=groups.length-1;i>=0;i--)<span style="white-space:pre"></span>if(groups[i].checked) hasChecked++;<span style="white-space:pre"></span>return min <= hasChecked && hasChecked <= max;<span style="white-space:pre"></span>},<span style="white-space:pre"></span>DoFilter : function(input, filter){return new RegExp("^.+\.(?=EXT)(EXT)$".replace(/EXT/g, filter.split(/\s*,\s*/).join("|")), "gi").test(input);<span style="white-space:pre"></span>},<span style="white-space:pre"></span>IsIdCard : function(number){<span style="white-space:pre"></span>var date, Ai;<span style="white-space:pre"></span>var verify = "10x98765432";<span style="white-space:pre"></span>var Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];<span style="white-space:pre"></span>var area = ['','','','','','','','','','','','北京','天津','河北','山西','内蒙古','','','','','','辽宁','吉林','黑龙江','','','','','','','','上海','江苏','浙江','安微','福建','江西','山东','','','','河南','湖北','湖南','广东','广西','海南','','','','重庆','四川','贵州','云南','西藏','','','','','','','陕西','甘肃','青海','宁夏','新疆','','','','','','台湾','','','','','','','','','','香港','澳门','','','','','','','','','国外'];<span style="white-space:pre"></span>var re = number.match(/^(\d{2})\d{4}(((\d{2})(\d{2})(\d{2})(\d{3}))|((\d{4})(\d{2})(\d{2})(\d{3}[x\d])))$/i);<span style="white-space:pre"></span>if(re == null) return false;<span style="white-space:pre"></span>if(re[1] >= area.length || area[re[1]] == "") return false;<span style="white-space:pre"></span>if(re[2].length == 12){<span style="white-space:pre"></span>Ai = number.substr(0, 17);<span style="white-space:pre"></span>date = [re[9], re[10], re[11]].join("-");<span style="white-space:pre"></span>}<span style="white-space:pre"></span>else{<span style="white-space:pre"></span>Ai = number.substr(0, 6) + "19" + number.substr(6);<span style="white-space:pre"></span>date = ["19" + re[4], re[5], re[6]].join("-");<span style="white-space:pre"></span>}<span style="white-space:pre"></span>if(!this.IsDate(date, "ymd")) return false;<span style="white-space:pre"></span>var sum = 0;<span style="white-space:pre"></span>for(var i = 0;i<=16;i++){<span style="white-space:pre"></span>sum += Ai.charAt(i) * Wi[i];<span style="white-space:pre"></span>}<span style="white-space:pre"></span>Ai +=  verify.charAt(sum%11);<span style="white-space:pre"></span>return (number.length ==15 || number.length == 18 && number == Ai);<span style="white-space:pre"></span>},<span style="white-space:pre"></span>IsDate : function(op, formatString){<span style="white-space:pre"></span>formatString = formatString || "ymd";<span style="white-space:pre"></span>var m, year, month, day;<span style="white-space:pre"></span>switch(formatString){<span style="white-space:pre"></span>case "ymd" :<span style="white-space:pre"></span>m = op.match(new RegExp("^((\\d{4})|(\\d{2}))([-./])(\\d{1,2})\\4(\\d{1,2})$"));<span style="white-space:pre"></span>if(m == null ) return false;<span style="white-space:pre"></span>day = m[6];<span style="white-space:pre"></span>month = m[5]*1;<span style="white-space:pre"></span>year =  (m[2].length == 4) ? m[2] : GetFullYear(parseInt(m[3], 10));<span style="white-space:pre"></span>break;<span style="white-space:pre"></span>case "dmy" :<span style="white-space:pre"></span>m = op.match(new RegExp("^(\\d{1,2})([-./])(\\d{1,2})\\2((\\d{4})|(\\d{2}))$"));<span style="white-space:pre"></span>if(m == null ) return false;<span style="white-space:pre"></span>day = m[1];<span style="white-space:pre"></span>month = m[3]*1;<span style="white-space:pre"></span>year = (m[5].length == 4) ? m[5] : GetFullYear(parseInt(m[6], 10));<span style="white-space:pre"></span>break;<span style="white-space:pre"></span>default :<span style="white-space:pre"></span>break;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>if(!parseInt(month)) return false;<span style="white-space:pre"></span>month = month==0 ?12:month;<span style="white-space:pre"></span>var date = new Date(year, month-1, day);        return (typeof(date) == "object" && year == date.getFullYear() && month == (date.getMonth()+1) && day == date.getDate());<span style="white-space:pre"></span>function GetFullYear(y){return ((y<30 ? "20" : "19") + y)|0;}<span style="white-space:pre"></span>} }</script>
</pre><pre name="code" class="js:nocontrols" style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 13px; line-height: 19.5px; background-color: rgb(255, 255, 255);">
</pre><pre name="code" class="js:nocontrols" style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 13px; line-height: 19.5px; background-color: rgb(255, 255, 255);">
</pre><pre name="code" class="js:nocontrols" style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 13px; line-height: 19.5px; background-color: rgb(255, 255, 255);">
</pre><pre name="code" class="js:nocontrols" style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 13px; line-height: 19.5px; background-color: rgb(255, 255, 255);">
</pre><pre name="code" class="js:nocontrols" style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 13px; line-height: 19.5px; background-color: rgb(255, 255, 255);">
</pre><pre name="code" class="js:nocontrols" style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 13px; line-height: 19.5px; background-color: rgb(255, 255, 255);">
</pre><pre name="code" class="js:nocontrols" style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 13px; line-height: 19.5px; background-color: rgb(255, 255, 255);">
</pre><pre name="code" class="js:nocontrols" style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 13px; line-height: 19.5px; background-color: rgb(255, 255, 255);">
</pre><pre name="code" class="js:nocontrols" style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 13px; line-height: 19.5px; background-color: rgb(255, 255, 255);">
</pre><pre name="code" class="js:nocontrols" style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 13px; line-height: 19.5px; background-color: rgb(255, 255, 255);">
-----------------------------------------------图片上传预览----------------------------------------------

<!DOCTYPE html>
<html>
<head>
<title>HTML5上传图片预览</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="http://www.codefans.net/ajaxjs/jquery-1.6.2.min.js"></script>
</head>
<body>
<h3>请选择图片文件:JPG/GIF</h3>
<form name="form0" id="form0" >
<input type="file" name="img" id="img" multiple="multiple"  onchange="imgPreview(this);" />
<hr>
<img src="" id="imgSpan"   style="width:200px;height:300px"   >
</form>
<script>
function imgPreview(obj) {
var url = null ;
var imgTag=$(obj).attr("id")+"Span";
if(obj.files!=undefined){
   var file=obj.files[0];
if (window.createObjectURL!=undefined) { // basic
url = window.createObjectURL(file) ;
} else if (window.URL!=undefined) { // mozilla(firefox)
url = window.URL.createObjectURL(file) ;
} else if (window.webkitURL!=undefined) { // webkit or chrome
url = window.webkitURL.createObjectURL(file) ;
}
$("#"+imgTag).attr("src", url) ;
} else { 
//IE下,使用滤镜 
var localImagId = document.getElementById(imgTag); 
obj.select(); 
obj.blur(); 
var imgSrc = document.selection.createRange().text; 
//必须设置初始大小 
localImagId.style.width = $("#"+imgTag).width(); 
localImagId.style.height = $("#"+imgTag).height(); 
//图片异常的捕捉,防止用户修改后缀来伪造图片 
try { 
localImagId.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)"; 
localImagId.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgSrc; 
$("#"+imgTag).attr("src", imgSrc) ;
} catch (e) { 
return;
        }



}
</script>
</body>
</html>

0 0