让IE8浏览器支持function.bind()方法
IE8支持function.bind()方法
<scripttype="text/javascript">
if(!Function.prototype.bind){
Function.prototype.bind=function(oThis){
if(typeofthis!=="function"){
thrownewTypeError("Function.prototype.bind-whatistryingtobeboundisnotcallable");
}
varaArgs=Array.prototype.slice.call(arguments,1),
fToBind=this,
fNOP=function(){},
fBound=function(){
returnfToBind.apply(thisinstanceoffNOP&&oThis
?this
:oThis,
aArgs.concat(Array.prototype.slice.call(arguments)));
};
fNOP.prototype=this.prototype;
fBound.prototype=newfNOP();
returnfBound;
};
}
</script>
主要解决“百度地图”官网上的例子的bug,摘取如下代码:
<!DOCTYPEhtml>
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<metaname="viewport"content="initial-scale=1.0,user-scalable=no"/>
<styletype="text/css">
body,html{width:100%;height:100%;margin:0;font-family:"微软雅黑";}
#allmap{width:100%;height:500px;}
p{margin-left:5px;font-size:14px;}
</style>
<scripttype="text/javascript"src="http://api.map.baidu.com/api?v=2.0&ak=39b92e64ae5622663ceceaccd8ab8eb1"></script>
<scriptsrc="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
<title>给多个点添加信息窗口</title>
<scripttype="text/javascript">
if(!Function.prototype.bind){
Function.prototype.bind=function(oThis){
if(typeofthis!=="function"){
thrownewTypeError("Function.prototype.bind-whatistryingtobeboundisnotcallable");
}
varaArgs=Array.prototype.slice.call(arguments,1),
fToBind=this,
fNOP=function(){},
fBound=function(){
returnfToBind.apply(thisinstanceoffNOP&&oThis
?this
:oThis,
aArgs.concat(Array.prototype.slice.call(arguments)));
};
fNOP.prototype=this.prototype;
fBound.prototype=newfNOP();
returnfBound;
};
}
</script>
</head>
<body>
<divid="allmap"></div>
<p>点击标注点,可查看由纯文本构成的简单型信息窗口</p>
</body>
</html>
<scripttype="text/javascript">
//百度地图API功能
map=newBMap.Map("allmap");
map.centerAndZoom(newBMap.Point(116.417854,39.921988),15);
vardata_info=[[116.417854,39.921988,"地址:北京市东城区王府井大街88号乐天银泰百货八层"],
[116.406605,39.921585,"地址:北京市东城区东华门大街"],
[116.412222,39.912345,"地址:北京市东城区正义路甲5号"]
];
varopts={
width:250,//信息窗口宽度
height:80,//信息窗口高度
title:"信息窗口",//信息窗口标题
enableMessage:true//设置允许信息窗发送短息
};
for(vari=0;i<data_info.length;i++){
varmarker=newBMap.Marker(newBMap.Point(data_info[i][0],data_info[i][1]));//创建标注
varcontent=data_info[i][2];
map.addOverlay(marker);//将标注添加到地图中
marker.addEventListener("click",openInfo.bind(null,content));
}
functionopenInfo(content,e){
varp=e.target;
varpoint=newBMap.Point(p.getPosition().lng,p.getPosition().lat);
varinfoWindow=newBMap.InfoWindow(content,opts);//创建信息窗口对象
map.openInfoWindow(infoWindow,point);//开启信息窗口
}
</script>