发布于 2016-11-12 00:04:24 | 174 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Javascript教程,程序狗速度看过来!

JavaScript客户端脚本语言

Javascript 是一种由Netscape的LiveScript发展而来的原型化继承的基于对象的动态类型的区分大小写的客户端脚本语言,主要目的是为了解决服务器端语言,比如Perl,遗留的速度问题,为客户提供更流畅的浏览效果。


这篇文章主要介绍了javascript在IE下trim函数无法使用的解决方法,分别叙述了javascript以及jQuery下的解决方案,对于WEB前端javascript设计人员进行浏览器兼容性调试有不错的借鉴价值,需要的朋友可以参考下

本文实例分析了javascript在IE下trim函数无法使用的解决方法,对于web前段设计有一定的借鉴价值。具体分析如下:

首先,javascript的trim函数在firefox下面使用没有问题:


<script language="javascript"> 
 var test1 = "  aa  "; 
 test1 = test1.toString(); 
 test1 = test1.trim(); 
</script>

在火狐下这样用没有问题, 但是在IE下就报错!

对此,我们可以修改一下:


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

在头上加上这一句,上面的就可以在IE和FF下都可以运行了:


<script language="javascript"> 
 String.prototype.trim=function(){return this.replace(/(^\s*)|(\s*$)/g,"");} 
 var test1 = "  aa  "; 
 test1 = test1.toString(); 
 test1 = test1.trim(); 
</script> 

JQuery提供的方法:


<!DOCTYPE html>  
<html>  
<head>  
 <script src="http://code.jquery.com/jquery-latest.js"></script>  
</head>  
<body>  
 <button>Show Trim Example</button>  
<script>  
$("button").click(function () {  
var str = "   lots of spaces before and after   ";  
alert("'" + str + "'");  
str = jQuery.trim(str);  
alert("'" + str + "' - no longer");  
});  
</script>  
</body>  
</html>

相信本文所述对大家利用javascript进行WEB前端浏览器的兼容性设计有不错的借鉴价值。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务