The jQuery selectors that we have explored so far allow us to select a set of elements as we navigate across and down the DOM tree and filter the results. If this were the only way to select elements, then our options would be quite limited (although, frankly, the selector expressions are robust in their own right, especially when compared to the regular DOM scripting options). There are many occasions when selecting a parent or ancestor element is essential; that is where jQuery's DOM traversal methods come into play. With these methods at our disposal, we can go up, down, and all around the DOM tree with ease.
一些方法的選取器運算式擁有幾乎相同的副本。比如,我們開始使用去添加alt類的代碼$("tr:even").addClass("alt"),可以使用.filter()重寫,就像下面這樣$("tr").filter(":even").addClass("alt"),然而在很多地方,這兩種選擇元素的方法互為補充。1. They must have a hrefattribute with a domain name (this.hostname). We use this test to exclude mailtolinks, for instance.
2. The domain name that they link to (again, this.hostname) must not match (!=) the domain name of the current page (location.hostname).
第二行代碼通過下面這兩個準則過濾了a元素集合:1、他們必須有一個href屬性,而且含有網域名稱(this.hostname)。我們使用這個測試排除類似於mailto連結。2、他們串連到的網域名稱必須不是當前頁的網域名稱地址(location.hostname)。More precisely, the .filter()method iterates through the matched set of elements, calling the function once for each, and testing the return value. If the function returns false, then the element is removed from the matched set. If it returns true, then the element is kept, as follows: