javascript用户点击
我想记录用户单击的元素(元素id或xpath),例如,如果它的a href记录xpath或id或文本。如何使用javascript实现这一点 我所拥有的: 点击: xpath:javascript用户点击,javascript,Javascript,我想记录用户单击的元素(元素id或xpath),例如,如果它的a href记录xpath或id或文本。如何使用javascript实现这一点 我所拥有的: 点击: xpath: function createXPathFromElement(elm) { var allNodes = document.getElementsByTagName('*'); for (segs = []; elm && elm.nodeType == 1; elm = elm.p
function createXPathFromElement(elm) {
var allNodes = document.getElementsByTagName('*');
for (segs = []; elm && elm.nodeType == 1; elm = elm.parentNode)
{
if (elm.hasAttribute('id')) {
var uniqueIdCount = 0;
for (var n=0;n < allNodes.length;n++) {
if (allNodes[n].hasAttribute('id') && allNodes[n].id == elm.id) uniqueIdCount++;
if (uniqueIdCount > 1) break;
};
if ( uniqueIdCount == 1) {
segs.unshift('id("' + elm.getAttribute('id') + '")');
return segs.join('/');
} else {
segs.unshift(elm.localName.toLowerCase() + '[@id="' + elm.getAttribute('id') + '"]');
}
} else if (elm.hasAttribute('class')) {
segs.unshift(elm.localName.toLowerCase() + '[@class="' + elm.getAttribute('class') + '"]');
} else {
for (i = 1, sib = elm.previousSibling; sib; sib = sib.previousSibling) {
if (sib.localName == elm.localName) i++; };
segs.unshift(elm.localName.toLowerCase() + '[' + i + ']');
};
};
return segs.length ? '/' + segs.join('/') : null;
};
function lookupElementByXPath(path) {
var evaluator = new XPathEvaluator();
var result = evaluator.evaluate(path, document.documentElement, null,XPathResult.FIRST_ORDERED_NODE_TYPE, null);
return result.singleNodeValue;
}
函数createXPathFromElement(elm){
var allNodes=document.getElementsByTagName('*');
对于(segs=[];elm&&elm.nodeType==1;elm=elm.parentNode)
{
if(elm.hasAttribute('id')){
var uniqueIdCount=0;
对于(var n=0;n1)中断;
};
如果(uniqueIdCount==1){
segs.unshift('id('+elm.getAttribute('id')+'));
返回segs.join('/');
}否则{
segs.unshift(elm.localName.toLowerCase()+”[@id=“”+elm.getAttribute('id')+“]);
}
}else if(elm.hasAttribute('class')){
segs.unshift(elm.localName.toLowerCase()+”[@class=“”+elm.getAttribute('class')+“]);
}否则{
对于(i=1,sib=elm.previousSibling;sib;sib=sib.previousSibling){
if(sib.localName==elm.localName)i++;};
segs.unshift(elm.localName.toLowerCase()++'['+i+']');
};
};
返回segs.length?'/'+segs.join('/'):null;
};
函数lookupElementByXPath(路径){
var evaluator=新的XPathEvaluator();
var result=evaluator.evaluate(路径,document.documentElement,null,XPathResult.FIRST\u ORDERED\u NODE\u TYPE,null);
返回result.singleNodeValue;
}
收听类似这样的点击事件
$(function() {
$(document).click(function(e) {
// do whatever you want to with the captured event
});
});
收听类似这样的点击事件
$(function() {
$(document).click(function(e) {
// do whatever you want to with the captured event
});
});
很容易获得单击对象的id
document.body.onclick = function(anEvent) {
console.log(anEvent.target.id)
};
如果使用jQuery,可以获得大量有关单击对象的信息
$("body").click(function(anEvent){ console.log(anEvent);});
这是否回答了您的问题?很容易获得单击的对象的id
document.body.onclick = function(anEvent) {
console.log(anEvent.target.id)
};
如果使用jQuery,可以获得大量有关单击对象的信息
$("body").click(function(anEvent){ console.log(anEvent);});
这回答了你的问题吗?我认为你最好试试IE<9中的
addEventListener
(或attachEvent
):
var processClick=函数(e)
{
e=e | | window.event;
var target=e.target | | e.src元素;
开关(target.tagName.toLowerCase())
{
案例“a”:
//使用target.href | | target.getAttribute('href');
返回e;//我认为您最好尝试addEventListener
(或IE<9中的attachEvent
):
var processClick=函数(e)
{
e=e | | window.event;
var target=e.target | | e.src元素;
开关(target.tagName.toLowerCase())
{
案例“a”:
//使用target.href | | target.getAttribute('href');
return e;//说明当前代码存在的问题。我不知道如何将两者结合起来,让脚本在用户单击的元素上记录xpath或referecne或诸如此类。不清楚您希望用此代码实现什么。说明当前代码存在的问题。我不知道如何将两者结合起来,要让脚本在用户单击的元素上记录xpath或referecne或etc,不清楚您希望用此代码实现什么。我是否错过了这里的jQuery标记?请不要假设jQuery。OP在问题的任何地方都没有使用它,也没有jQ标记。不是每个人都使用它。我是否错过了这里的jQuery标记?请,不要假设jQuery。OP没有在问题的任何地方使用它,也没有jQ标记。不是每个人都使用它