講兩件事:1.this指針的用法小探. 2.ie的attachEvent和firefox的addEventListener在事件處理上的區(qū)別
更新時間:2007年04月12日 00:00:00 作者:
第一件事情.
this 指鐘是JavaScript語言中的一個特殊指鐘,他在代碼運行時,指向調(diào)用this語句的當(dāng)前對象.
如果是事件綁定函數(shù),則指向被綁定的元素本身.
<script type="text/javascript">
//by Go_Rush(阿舜) from http://ashun.cnblogs.com/
alert(this===window) //true 直
接調(diào)用的時候,指向window本身
var gorush={
f:function(){
alert(this===gorush) //true
}
}
gorush.f() //指向 gorush對象
document.onclick=function(){
alert(this===document) //true ,指向 document
}
/*
element.onclick=function(){
alert(this===element) //true
}
*/
</script>
特別要值得注意的是,當(dāng)多個對象嵌套的時候, this 是指向最近調(diào)用它的那個對象的
obj1={
obj2:{
f:function(){
alert(this===obj1.obj2) //這里 this 并不是指向 obj1的哦。
}
}
}
obj1.obj2.f()
再舉一個非常容易出錯的例子, 點這里看相關(guān)鏈接
<script type="text/javascript">
//by Go_Rush from http://ashun.cnblogs.com/
//以下gorush1中 this的用法是錯誤的,這個錯誤10個程序員6個犯
var gorush1={
showMsg:function(){alert("hello,world")},
doAjax:function(){
new Ajax.Request("index.php",{onSuccess:function(){
this.showMsg()
}})
}
}
//gorush2中的才是對的
var gorush2={
showMsg:function(){alert("hello,world")},
doAjax:function(){
var self=this; //備份 gorush2對象
new Ajax.Request("index.php",{onSuccess:function(){
self.showMsg()
}})
}
}
</script>
第二件事情:
閑話不多說,先上碟小菜.
<script type="text/javascript">
var btn=null
window.onload=function(){
btn=document.getElementById("btn")
if (window.attachEvent) btn.attachEvent("onclick",gorush);
if (window.addEventListener) btn.addEventListener("click",gorush,false)
}
function gorush(){
if (this===window) alert("this==window") //ie6.0下,這句會執(zhí)行
if (this===btn) alert("this==btn") //ff1.5下, 這句會執(zhí)行
}
</script>
<input type="button" value="click me" id="btn">
真不明白為什么 ie 會這樣搞,讓人很郁悶啊,為什么把 this 指向 window呢?
解決方法:
1. 事件綁定的時候不要用 attachEvent, 可憐的我,當(dāng)時就是用的prototype.js的Event.Observe方法
這樣 element.onclick=function..... 這樣在兩個瀏覽器中 this 指鐘都指向 element
2. 在處理函數(shù) gorush中 用 getEvent()方法統(tǒng)一獲取事件,然后在用 evt.srcElement || evt.target 獲取 element對象
this 指鐘是JavaScript語言中的一個特殊指鐘,他在代碼運行時,指向調(diào)用this語句的當(dāng)前對象.
如果是事件綁定函數(shù),則指向被綁定的元素本身.
<script type="text/javascript">
//by Go_Rush(阿舜) from http://ashun.cnblogs.com/
alert(this===window) //true 直
接調(diào)用的時候,指向window本身
var gorush={
f:function(){
alert(this===gorush) //true
}
}
gorush.f() //指向 gorush對象
document.onclick=function(){
alert(this===document) //true ,指向 document
}
/*
element.onclick=function(){
alert(this===element) //true
}
*/
</script>
特別要值得注意的是,當(dāng)多個對象嵌套的時候, this 是指向最近調(diào)用它的那個對象的
obj1={
obj2:{
f:function(){
alert(this===obj1.obj2) //這里 this 并不是指向 obj1的哦。
}
}
}
obj1.obj2.f()
再舉一個非常容易出錯的例子, 點這里看相關(guān)鏈接
<script type="text/javascript">
//by Go_Rush from http://ashun.cnblogs.com/
//以下gorush1中 this的用法是錯誤的,這個錯誤10個程序員6個犯
var gorush1={
showMsg:function(){alert("hello,world")},
doAjax:function(){
new Ajax.Request("index.php",{onSuccess:function(){
this.showMsg()
}})
}
}
//gorush2中的才是對的
var gorush2={
showMsg:function(){alert("hello,world")},
doAjax:function(){
var self=this; //備份 gorush2對象
new Ajax.Request("index.php",{onSuccess:function(){
self.showMsg()
}})
}
}
</script>
第二件事情:
閑話不多說,先上碟小菜.
<script type="text/javascript">
var btn=null
window.onload=function(){
btn=document.getElementById("btn")
if (window.attachEvent) btn.attachEvent("onclick",gorush);
if (window.addEventListener) btn.addEventListener("click",gorush,false)
}
function gorush(){
if (this===window) alert("this==window") //ie6.0下,這句會執(zhí)行
if (this===btn) alert("this==btn") //ff1.5下, 這句會執(zhí)行
}
</script>
<input type="button" value="click me" id="btn">
真不明白為什么 ie 會這樣搞,讓人很郁悶啊,為什么把 this 指向 window呢?
解決方法:
1. 事件綁定的時候不要用 attachEvent, 可憐的我,當(dāng)時就是用的prototype.js的Event.Observe方法
這樣 element.onclick=function..... 這樣在兩個瀏覽器中 this 指鐘都指向 element
2. 在處理函數(shù) gorush中 用 getEvent()方法統(tǒng)一獲取事件,然后在用 evt.srcElement || evt.target 獲取 element對象
您可能感興趣的文章:
- Javascript 的addEventListener()及attachEvent()區(qū)別分析
- addEventListener 的用法示例介紹
- window.addEventListener來解決讓一個js事件執(zhí)行多個函數(shù)
- document.addEventListener使用介紹
- JS在IE和FF下attachEvent,addEventListener學(xué)習(xí)筆記
- 事件綁定之小測試 onclick && addEventListener
- javascript attachEvent和addEventListener使用方法
- 詳解addEventListener的三個參數(shù)之useCapture
- addEventListener()第三個參數(shù)useCapture (Boolean)詳細解析
- addEventListener()與removeEventListener()解析
相關(guān)文章
JavaScript獲取GridView中用戶點擊控件的行號,列號
GridView中的某幾列有按鈕,需要獲取用戶當(dāng)前點的按鈕的行號(捎帶的得到列號)2009-04-04使用element-plus時重寫樣式不起作用的問題及解決方法
這篇文章給大家介紹使用element-plus時重寫樣式不起作用的問題及解決方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-09-09使用Javascript實現(xiàn)復(fù)制粘貼功能的示例代碼
本篇文章記錄如何通過js代碼實現(xiàn)復(fù)制內(nèi)容到剪切板,之后可以粘貼到需要的地方的功能,文中通過代碼示例介紹的非常詳細,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-09-09js 剪切板的用法(clipboardData.setData)與js match函數(shù)介紹
這篇文章主要是對js中剪切板的使用方法(clipboardData.setData)與js中的match函數(shù)進行了介紹。需要的朋友可以過來參考下,希望對大家有所幫助2013-11-11