JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.querySelectorALL("span[data-value='CNStockPC']")")[1].classList.add("focused")
給這個span添加一個focused的class。
--------------------------------------------
javascript中的幾個有用定位
1. document.querySelectorAll("span[data-value='CN']")[1].parentElement
找到元素后往上找到它的父類元素
2.?document.querySelectorAll("span[data-value='CN']")[1].closest(".cdk-abc")
往上找class為.cdk-abc的元素的最近元素
----------------------
document.querySelector(selector)
從整個文檔中查找第一個匹配 CSS 選擇器的元素,返回單個元素(無匹配則返回?null)。
例:document.querySelector('div.className')?找第一個帶指定類的 div。
document.querySelectorAll(selector)
從整個文檔中查找所有匹配 CSS 選擇器的元素,返回類數(shù)組(NodeList)。
例:document.querySelectorAll('ul li')?找所有 ul 下的 li 元素。
element.closest(selector)
從當前元素開始,向上遍歷 DOM 樹(包括自身),查找最近的一個匹配選擇器的祖先元素,返回該元素(無匹配則返回?null)。
例:child.closest('.parent')?從子元素找最近的帶 parent 類的祖先。
element.querySelector(selector)
在當前元素的子節(jié)點中,查找第一個匹配選擇器的元素(僅搜索后代,不包括自身)。
例:container.querySelector('span.active')?在容器內(nèi)找第一個激活的 span。
element.querySelectorAll(selector)
在當前元素的子節(jié)點中,查找所有匹配選擇器的元素,返回 NodeList。
例:list.querySelectorAll('li:nth-child(odd)')?找列表中所有奇數(shù)位置的 li。
element.matches(selector)
判斷當前元素是否匹配指定選擇器,返回布爾值(true/false)。
例:el.matches('.active')?檢查元素是否有 active 類。
const stickyNav = function(e){
? const [entry] = e;
? console.log(e);
}
const header = document.querySelector('.header');
const headerObserver = new IntersectionObserver(stickyNav,{
? root:null,
? threshold:0
});
headerObserver.observe(header);
1. 創(chuàng)建一個intersectionObserver()對象,里面有兩個參數(shù),方法和選項
const headerObserver = new IntersectionObserver(Observerfunction,Option});
const Oberverfunction =?function(e){
? const entry = e[0];? ? ? 從數(shù)據(jù)e中提取第一個
? console.log(e);
}
const Option = {root: null,threshold: 0}
root:null指默認的根元素是視口viewport
threshold: 0 定義觸發(fā)回調(diào)的交叉比例閾值(范圍0-1), 0當目標元素剛進入根元素或者剛離開根元素時,立刻觸發(fā)回調(diào)函數(shù)observerfunction
2. 運行此對象,傳入需要觀察的對象
headerObserver(header)