js相关问题的解决方案

来源:互联网 发布:linux 网卡物理地址 编辑:程序博客网 时间:2024/06/05 14:37

1、IE9 button 超链接 无效解决方案

<a href="<%=path %>/mobileView/demo.html"><input type="button" value="配置" onclick="this.parentNode.click()" class="input_srue" /></a>

onclick="this.parentNode.click()" 很关键


2、超链接跳出iframe方法:将超连接加入 target=_top 即可


3、js将选择的时间与当前时间比较

var str ='2012-08-12 23:13:15';str = str.replace(/-/g,"/");var date = new Date(str );
var curDate = new Date();

if(date<curDate)   


4、session过期跳出iframe

login.jsp 里增加

<script language="JavaScript">
if (window != top)
top.location.href = location.href;
</script>


5、js编码与解码

encodeURI() 函数可把字符串作为 URI 进行编码。

解码为decodeURI()


6、js substring与substr的区别


在JS中, 函数声明: stringObject.substring(start,stop)

  start是在原字符串检索的开始位置,stop是检索的终止位置,返回结果中不包括stop所指字符.JS中的substring和substr函数的区别

如:"hamburger".substring(4,8) returns "urge"

 

substr(start,length)

如:当DETAIL_TYPE 的值为1111053 时,
substr(DETAIL_TYPE,1,6)  =111105 
从DETAIL_TYPE的第一位开始取六位字符。


7、js图片处理

js获取图片的高宽:【img.height 图片高度为0时,是因为图片有可能还未加载完毕】

var img = new Image();
            img.src=imgServer+"/"+retData.con.filename+"."+retData.con.ext;
            img.onload = function(){
                $("#bigBrandForm").append("<input type='hidden' id='secondPicHeightToWidth' value='"+img.height/img.width+"'/>");
            };


图片缩放:<img id="imgPath_pic" onload="javascript:if(this.width>200){this.width=200;this.height=this.height*200/this.width;}" src="" />



8、jsp页面的table格中的字自动换行

<style type="text/css"> 
.AutoNewline 

  Word-break: break-all;/*必须*/ 
  width :50px;/*这里是设置多宽就进行换行 */ 

</style>



9、js json与jsonp

js  ajax 调用接口时如果dataType为json时不能跨域访问,则反馈回来的数据就是json串格式,可直接解析。可处理特殊字符

$.ajax({
            url:root+"/***.rq?rmethod=index",
            dataType:"json",
    
            success:function(data){
                if(data)
                    setData(data);
            }
        });


如果dataType为jsonp时能跨域访问,返回的数据需要用eval或JSON.parse转换成对象,方可直接解析,jsonp对于特殊字符如单引号(')等无法转义。


$.ajax({
            url:root+"/izp.rq?rmethod=index",
            dataType:"jsonp",
            jsonp:"callback",
            success:function(data){
                if(data)
                    setData(JSON.parse(data));
            }
        });

10、js replaceAll  实现

String.prototype.replaceAll  = function(s1,s2){  
return this.replace(new RegExp(s1,"gm"),s2);   //这里的gm是固定的,g可能表示global,m可能表示multiple。
}
调用方式:
如想替换字符串"aa bb cc"中的所有空格,可以执行"aa bb cc".replaceAll(" ", "");

0 0
原创粉丝点击