如何捕获用户用JavaScript点击的* ANY *元素? [英] How to capture *ANY* element where the user clicked with JavaScript?

查看:150
本文介绍了如何捕获用户用JavaScript点击的* ANY *元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是的后续行动问题。

这个想法是让用户通过JavaScript在Firefox浏览器中点击的地方。
从指定的问题开始,下面的 JavaScript 代码几乎就是这样:

The idea is to get where the user clicked in a Firefox browser through a JavaScript. Starting from the named question, the following JavaScript code almost does it:

var DocElements = document.getElementsByTagName('*');for(var i = 0; i < DocElements.length; i++){DocElements[i].onclick = function clicked(MyElement){var target = (MyElement.target) ? MyElement.target : MyElement.srcElement;var e = document.createElement('a'); e.setAttribute('id','myUniqueID'); e.setAttribute('value', this); target.appendChild(e);};}

这个脚本插入一个id为myUniqueID的子元素任何元素点击,然后脚本可以通过执行以下 python 代码来检索点击的文档:

This script inserts a child with id "myUniqueID" in any element clicked, then the script can retrieve the clicked documents by doing the following python code:

resultss=browser.driver.find_element_by_id("myUniqueID") #This uses selenium driver engine.
resultss.find_element_by_xpath("..")  #This gets the parent node of selected one

JS代码旨在挑选在网站上点击的任何元素,通过附加到html代码中的所有项目:

The JS Code aims to pick any element clicked on the website, by appending itself to all the items in the html code:

document.getElementsByTagName('*')

但是这个脚本失败了,它无法挑选所有的元素。例如,如果您在Google注册页面中点击月/性别表单,然后选择一个月,它会选择代表所选月份的元素,但是没有检测到第一次点击表单。因此,getElementsByTagName('*')并不是挑选网站中的所有元素。

But this script fails, because its not able to pick all the elements. For example, if you click in the month / gender form in google signup page,then select one month, it will pick the element that represents the selected month, but doesnt detect the first click on the form. Therefore, getElementsByTagName('*') is not picking all the elements in the website.

Javascript如何连接到 ALL 在一个文档中的元素,没有例外?

How can the Javascript be attached running for ALL the elements in a document, without exceptions?

推荐答案

我聘请了一个自由职业者完成脚本。我非常接近,但由于即时通讯在Javascript中是全新的,所以要花太多的时间才能弄明白。我需要使用onmousedown而不是onclick作为事件。生成的脚本如下:

I hired a freelancer to finish the script. I was pretty close, but since im absolutely new in Javascript, it would have taken me too long to figure it out. I was needing to use 'onmousedown' instead of 'onclick' as event. The resulting script is as follows:

var DocElements = document.getElementsByTagName('*');for(var i = 0; i < DocElements.length; i++){DocElements[i].onmousedown = function clicked(MyElement){var target = (MyElement.target) ? MyElement.target : MyElement.srcElement;var e = document.createElement('a'); e.setAttribute('id','myUniqueID'); e.setAttribute('value', this); target.appendChild(e);};}

我希望这对其他人有用。

I hope this is useful for someone else in the future.

这篇关于如何捕获用户用JavaScript点击的* ANY *元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆