讓IE8瀏覽器支持function.bind()方法
更新時(shí)間:2014年10月16日 17:21:15 投稿:whsnow
function.bind()方法默認(rèn)IE8是不支持的,下面有個(gè)小技巧可完美解決這個(gè)問(wèn)題,為此疑惑的朋友可以看看
IE8支持function.bind()方法
<script type="text/javascript">
if (!Function.prototype.bind) {
Function.prototype.bind = function (oThis) {
if (typeof this !== "function") {
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
}
var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function () {},
fBound = function () {
return fToBind.apply(this instanceof fNOP && oThis
? this
: oThis,
aArgs.concat(Array.prototype.slice.call(arguments)));
};
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
return fBound;
};
}
</script>
主要解決“百度地圖”官網(wǎng)上的例子的bug,摘取如下代碼:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="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>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=39b92e64ae5622663ceceaccd8ab8eb1"></script>
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
<title>給多個(gè)點(diǎn)添加信息窗口</title>
<script type="text/javascript">
if (!Function.prototype.bind) {
Function.prototype.bind = function (oThis) {
if (typeof this !== "function") {
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
}
var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function () {},
fBound = function () {
return fToBind.apply(this instanceof fNOP && oThis
? this
: oThis,
aArgs.concat(Array.prototype.slice.call(arguments)));
};
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
return fBound;
};
}
</script>
</head>
<body>
<div id="allmap"></div>
<p>點(diǎn)擊標(biāo)注點(diǎn),可查看由純文本構(gòu)成的簡(jiǎn)單型信息窗口</p>
</body>
</html>
<script type="text/javascript">
// 百度地圖API功能
map = new BMap.Map("allmap");
map.centerAndZoom(new BMap.Point(116.417854,39.921988), 15);
var data_info = [[116.417854,39.921988,"地址:北京市東城區(qū)王府井大街88號(hào)樂(lè)天銀泰百貨八層"],
[116.406605,39.921585,"地址:北京市東城區(qū)東華門(mén)大街"],
[116.412222,39.912345,"地址:北京市東城區(qū)正義路甲5號(hào)"]
];
var opts = {
width : 250, // 信息窗口寬度
height: 80, // 信息窗口高度
title : "信息窗口" , // 信息窗口標(biāo)題
enableMessage:true//設(shè)置允許信息窗發(fā)送短息
};
for(var i=0;i<data_info.length;i++){
var marker = new BMap.Marker(new BMap.Point(data_info[i][0],data_info[i][1])); // 創(chuàng)建標(biāo)注
var content = data_info[i][2];
map.addOverlay(marker); // 將標(biāo)注添加到地圖中
marker.addEventListener("click",openInfo.bind(null,content));
}
function openInfo(content,e){
var p = e.target;
var point = new BMap.Point(p.getPosition().lng, p.getPosition().lat);
var infoWindow = new BMap.InfoWindow(content,opts); // 創(chuàng)建信息窗口對(duì)象
map.openInfoWindow(infoWindow,point); //開(kāi)啟信息窗口
}
</script>
您可能感興趣的文章:
- jQuery事件綁定.on()簡(jiǎn)要概述及應(yīng)用
- jquery移除、綁定、觸發(fā)元素事件使用示例詳解
- jQuery中的.bind()、.live()和.delegate()之間區(qū)別分析
- JQuery中綁定事件(bind())和移除事件(unbind())
- JQuery中Bind()事件用法分析
- jQuery事件綁定on()、bind()與delegate() 方法詳解
- Query中click(),bind(),live(),delegate()的區(qū)別
- 淺談javascript中call()、apply()、bind()的用法
- jQuery綁定事件方法及區(qū)別(bind,click,on,live,one)
相關(guān)文章
如何利用JavaScript編寫(xiě)更好的條件語(yǔ)句詳解
這篇文章主要給大家介紹了關(guān)于如何利用JavaScript編寫(xiě)更好的條件語(yǔ)句的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用JavaScript具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Js注冊(cè)協(xié)議倒計(jì)時(shí)的小例子
Js注冊(cè)協(xié)議倒計(jì)時(shí)的小例子,需要的朋友可以參考一下2013-06-06
js動(dòng)態(tài)設(shè)置div的值下例子
設(shè)置div的值想必大家都會(huì)吧,按要說(shuō)動(dòng)態(tài)設(shè)置想必知道的人及寥寥無(wú)幾了,下面有個(gè)不錯(cuò)的示例,希望對(duì)大家有所幫助2013-10-10
JavaScript實(shí)現(xiàn)鼠標(biāo)經(jīng)過(guò)表格行給出顏色標(biāo)識(shí)
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)鼠標(biāo)經(jīng)過(guò)表格行給出顏色標(biāo)識(shí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04
javaScript實(shí)現(xiàn)游戲倒計(jì)時(shí)功能
這篇文章主要為大家詳細(xì)介紹了javaScript實(shí)現(xiàn)游戲倒計(jì)時(shí)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
微信小程序?qū)崿F(xiàn)短信登錄的實(shí)戰(zhàn)
項(xiàng)目要求增加短信登錄及人臉識(shí)別登錄功能,本文就來(lái)實(shí)現(xiàn)一下 短信登錄功能,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10

