我只是先看jquery,因爲它是JavaScript。下面是實際的代碼:
if (jQuery.expr && jQuery.expr.filters) {
// here is the real guts of it
jQuery.expr.filters.hidden = function(elem) {
// plain old JavaScript determining offset
var width = elem.offsetWidth,
height = elem.offsetHeight;
// if any of these are "true" then its "invisible"
return (width === 0 && height === 0) ||
(!jQuery.support.reliableHiddenOffsets &&
((elem.style && elem.style.display) ||
jQuery.css(elem, "display")) === "none");
};
// this is just checking for not hidden
jQuery.expr.filters.visible = function(elem) {
return !jQuery.expr.filters.hidden(elem);
};
}
的「reliableHiddenOffsets」代碼在此之前定義的方式,你可以看到它下面
isSupported = (tds[ 0 ].offsetHeight === 0);
tds[ 0 ].style.display = "";
tds[ 1 ].style.display = "none";
// Check if empty table cells still have offsetWidth/Height
// (IE <= 8 fail this test)
support.reliableHiddenOffsets = isSupported && (tds[ 0 ].offsetHeight === 0);
真正的教訓是這個東西是不是火箭科學。破解jQuery並看看。 jQuery的真正魅力在於它經過了嚴格的測試和打擊,你可能不會發現任何問題。除了偉大的選擇器引擎和抽象之外,這是值得的。不要害怕真正看。你會在這個過程中學到一些好的副作用。
爲什麼你打擾,如果你有jQuery的? – dynamic 2012-03-09 16:56:31
'知名度:隱藏;'不是等式的一部分?不知道一個等價物,但這是一個簡短的函數。 - @ yes123有時你需要一個函數而不是所有的核心框架作爲開銷。 – Smamatti 2012-03-09 16:57:42
[這裏](https://github.com/jquery/jquery/blob/master/src/css.js#L380)jQuery檢查元素是否通過選擇器。 – pimvdb 2012-03-09 17:00:20