亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

JQuery this 和 $(this) 的區(qū)別

 更新時間:2009年08月23日 19:00:13   作者:  
起初以為this和$(this)就是一模子刻出來。但是我在閱讀時,和coding時發(fā)現(xiàn),總不是一回事。
What is "this"?
In many object-oriented programming languages, this (or self) is a keyword which can be used in instance methods to refer to the object on which the currently executing method has been invoked.
復(fù)制代碼 代碼如下:

$("#textbox").hover(
function() {
this.title = "Test";
},
fucntion() {
this.title = "OK”;
}
);

這里的this其實是一個Html 元素(textbox),textbox有text屬性,所以這樣寫是完全沒有什么問題的。
但是如果將this換成$(this)就不是那回事了,Error–報了。
Error Code:
復(fù)制代碼 代碼如下:

$("#textbox").hover(
function() {
$(this).title = "Test";
},
function() {
$(this).title = "OK";
}
);

這里的$(this)是一個JQuery對象,而jQuery對象沒有title 屬性,因此這樣寫是錯誤的。

JQuery擁有attr()方法可以get/set DOM對象的屬性,所以正確的寫法應(yīng)該是這樣:

正確的代碼:
復(fù)制代碼 代碼如下:

$("#textbox").hover(
function() {
$(this).attr('title', ‘Test');
},
function() {
$(this).attr('title', ‘OK');
}
);

使用JQuery的好處是它包裝了各種瀏覽器版本對DOM對象的操作,因此統(tǒng)一使用$(this)而不再用this應(yīng)該是比較不錯的選擇。

相關(guān)文章

最新評論