假設我在寫一個jQuery擴展方法。該方法應該爬一個元素的祖先樹,直到它到達文檔的根<html>
標記,此時它應該停止。我實現了這個如下所示:jQuery:確定jQuery對象是否爲根html標記
$.fn.foo = function() {
var $foo = this;
while($foo[0] !== $(document).children()[0]) {
// do stuff with $foo
$foo = $foo.parent();
}
// do stuff
};
我的問題是:是否有比$foo[0] !== $(document).children()[0]
更好的辦法知道是否我已經達到了根<html>
標籤?
該插件是否也應該使用''元素? – Anurag 2010-09-03 22:40:35