1
請解釋一下,爲什麼this
的行爲與我自己有所不同?jQuery`this`在用戶函數中的工作方式怪異
(function ($) {
$.fn.wtf = function() {
// See in console
// `this` returns what expected
console.log(this);
// `this.html()` returns only the first element's content
console.log(this.html());
}
$('body').find(':header').wtf();
}(jQuery));
https://jsfiddle.net/ta7Lmf7s/1/
你期望什麼? –
這就是'html()'顯式做的事情,[docs](http://api.jquery.com/html/)的第一行之一明確指出*「如果選擇器表達式匹配多個元素,只有第一場比賽將其HTML內容返回「* – adeneo
爲什麼不告訴我們你在這裏做什麼,也許你會得到一個解決問題的答案 – adeneo