首页 > 语言 > JavaScript > 正文

让IE8浏览器支持function.bind()方法

2024-05-06 16:09:47
字体:
来源:转载
供稿:网友

function.bind()方法默认IE8是不支持的,下面有个小技巧可完美解决这个问题,为此疑惑的朋友可以看看

IE8支持function.bind()方法

  1. <script type="text/javascript">  
  2. if (!Function.prototype.bind) {  
  3. Function.prototype.bind = function (oThis) {  
  4. if (typeof this !== "function") {  
  5. throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");  
  6. }  
  7. var aArgs = Array.prototype.slice.call(arguments, 1),  
  8. fToBind = this,  
  9. fNOP = function () {},  
  10. fBound = function () {  
  11. return fToBind.apply(this instanceof fNOP && oThis  
  12. this 
  13. : oThis,  
  14. aArgs.concat(Array.prototype.slice.call(arguments)));  
  15. };  
  16. fNOP.prototype = this.prototype;  
  17. fBound.prototype = new fNOP();  
  18. return fBound;  
  19. };  
  20. }  
  21. </script> 

主要解决“百度地图”官网上的例子的bug,摘取如下代码:

 

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  5. <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />  
  6. <style type="text/css">  
  7. body, html {width: 100%;height: 100%;margin:0;font-family:"微软雅黑";} 
  8. #allmap{width:100%;height:500px;}  
  9. p{margin-left:5px; font-size:14px;}  
  10. </style>  
  11. <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=39b92e64ae5622663ceceaccd8ab8eb1"></script>  
  12. <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>  
  13. <title>给多个点添加信息窗口</title>  
  14. <script type="text/javascript">  
  15. if (!Function.prototype.bind) {  
  16. Function.prototype.bind = function (oThis) {  
  17. if (typeof this !== "function") {  
  18. throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");  
  19. }  
  20. var aArgs = Array.prototype.slice.call(arguments, 1),  
  21. fToBind = this,  
  22. fNOP = function () {},  
  23. fBound = function () {  
  24. return fToBind.apply(this instanceof fNOP && oThis  
  25. this 
  26. : oThis,  
  27. aArgs.concat(Array.prototype.slice.call(arguments)));  
  28. };  
  29. fNOP.prototype = this.prototype;  
  30. fBound.prototype = new fNOP();  
  31. return fBound;  
  32. };  
  33. }  
  34. </script>  
  35. </head>  
  36. <body>  
  37. <div id="allmap"></div>  
  38. <p>点击标注点,可查看由纯文本构成的简单型信息窗口</p>  
  39. </body>  
  40. </html>  
  41. <script type="text/javascript">  
  42. // 百度地图API功能  
  43. map = new BMap.Map("allmap");  
  44. map.centerAndZoom(new BMap.Point(116.417854,39.921988), 15);  
  45. var data_info = [[116.417854,39.921988,"地址:北京市东城区王府井大街88号乐天银泰百货八层"],  
  46. [116.406605,39.921585,"地址:北京市东城区东华门大街"],  
  47. [116.412222,39.912345,"地址:北京市东城区正义路甲5号"]  
  48. ];  
  49. var opts = {  
  50. width : 250, // 信息窗口宽度  
  51. height: 80, // 信息窗口高度  
  52. title : "信息窗口" , // 信息窗口标题  
  53. enableMessage:true//设置允许信息窗发送短息  
  54. };  
  55. for(var i=0;i<data_info.length;i++){  
  56. var marker = new BMap.Marker(new BMap.Point(data_info[i][0],data_info[i][1])); // 创建标注  
  57. var content = data_info[i][2];  
  58. map.addOverlay(marker); // 将标注添加到地图中  
  59. marker.addEventListener("click",openInfo.bind(null,content));  
  60. }  
  61. function openInfo(content,e){  
  62. var p = e.target;  
  63. var point = new BMap.Point(p.getPosition().lng, p.getPosition().lat);  
  64. var infoWindow = new BMap.InfoWindow(content,opts); // 创建信息窗口对象  
  65. map.openInfoWindow(infoWindow,point); //开启信息窗口  
  66. }  
  67. </script> 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选