互動式直譯器變數
請注意,這些僅適用於某些瀏覽器的開發人員工具。
$_ 為你提供最後評估的任何表示式的值。
"foo" // "foo"
$_ // "foo"
$0 指的是 Inspector 中當前選中的 DOM 元素。所以如果 <div id="foo"> 突出顯示:
$0 // <div id="foo">
$0.getAttribute('id') // "foo"
$1 指的是先前選擇的元素,$2 指之前選擇的元素,以及 $3 和 $4 等等。
要獲取與 CSS 選擇器匹配的元素集合,請使用 $$(selector)。這實際上是 document.querySelectorAll 的捷徑。
var images = $$('img'); // Returns an array or a nodelist of all matching elements
| $ _ | $()¹ | $$() | $ 0 | $ 1 | $ 2 | $ 3 | $ 4 | |
|---|---|---|---|---|---|---|---|---|
Opera |
15+ | 11+ | 11+ | 11+ | 11+ | 15+ | 15+ | 15+ |
Chrome |
22+ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
Firefox |
39+ | ✓ | ✓ | ✓ | × | × | × | × |
IE |
11 | 11 | 11 | 11 | 11 | 11 | 11 | 11 |
| 蘋果瀏覽器 | 6.1+ | 4+ | 4+ | 4+ | 4+ | 4+ | 4+ | 4+ |
¹ 別名為 document.getElementById 或 document.querySelector