js代碼,鼠標(biāo)在頁(yè)面點(diǎn)擊時(shí),記錄元素的Xpath
代碼:
function mouse_down(event){
var x = event.clientX, y = event.clientY
var element = document.elementFromPoint(x, y)
if (!element) {
console.log("error: no element")
}
console.log(readXPath(element))
}
function readXPath(element) {
count = 1
result = ""
while (true){
count += 1
if(count>99){
break
}
if (element == document.body){
console.log('/html/body/' + result)
break
}
else
{
tag_index = 0
tmp = element.parentElement
for(var i=0;i<tmp.childElementCount;i++){
if (tmp.children[i].tagName == element.tagName){
tag_index += 1
}
if (element == tmp.children[i]){
result = element.tagName.toLowerCase() + "[" + String(tag_index) + "]" + "/" +result
// console.log(result)
break
}
}
element = tmp
}
}
}
document.onmousedown = mouse_down
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-555134.html
?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-555134.html
到了這里,關(guān)于JS javascript 點(diǎn)擊鼠標(biāo) 鼠標(biāo)事件 獲取元素 獲取元素Xpath的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!