简体   繁体   English

javascript查找元素的选择器

[英]javascript find selector of an element

what i want is to detect which elements clicks by user and send those selector query to server我想要的是检测用户点击了哪些元素并将这些选择器查询发送到服务器
I don't want to use any library and want to do this only by javascript itself, my code listen document.onclick and I could get event.target and do some stuff in real time, but I want store this element selector query in server for future usage我不想使用任何库,只想通过 javascript 本身来执行此操作,我的代码监听document.onclick ,我可以获取event.target并实时做一些事情,但我想将此元素选择器查询存储在服务器中供将来使用

document.onclick = function (event) {
    var target = event.target;
    var selector ; // 
}

how get event.target unique selector?如何获取event.target唯一选择器? and use this selector in document.querySelector and access to element并在document.querySelector中使用此选择器并访问元素

I built a function in Javascript without Jquery that creates a unique identifier for any element in the DOM. 我在没有Jquery的Javascript中构建了一个函数,该函数为DOM中的任何元素创建唯一标识符。 If it has an id it uses the id. 如果具有ID,则使用该ID。 Otherwise it creates a path based on the parents of that element: 否则,它将基于该元素的父级创建路径:

function getSelector(node) {
    var id = node.getAttribute('id');

    if (id) {
        return '#'+id;
    }

    var path = '';

    while (node) {
        var name = node.localName;
        var parent = node.parentNode;

        if (!parent) {
            path = name + ' > ' + path;
            continue;
        }

        if (node.getAttribute('id')) {
            path = '#' + node.getAttribute('id') + ' > ' + path;
            break;
        }

        var sameTagSiblings = [];
        var children = parent.childNodes;
        children = Array.prototype.slice.call(children);

        children.forEach(function(child) {
            if (child.localName == name) {
                sameTagSiblings.push(child);
            }
        });

        // if there are more than one children of that type use nth-of-type

        if (sameTagSiblings.length > 1) {
            var index = sameTagSiblings.indexOf(node);
            name += ':nth-of-type(' + (index + 1) + ')';
        }

        if (path) {
            path = name + ' > ' + path;
        } else {
            path = name;
        }

        node = parent;
    }

    return path;
}

So if you want to click on an element and get the unique selector just add: 因此,如果您想单击一个元素并获得唯一选择器,只需添加:

window.addEventListener('click', function(event) {
    var ele = document.elementFromPoint(event.x, event.y);
    alert(getSelector(ele));
});

I suppose you mean a CSS selector. 我想你的意思是一个CSS选择器。 If so, it can be selected through multiple methods. 如果是这样,可以通过多种方法选择它。 If it has an id you can simply get the id from the element: 如果它具有ID,则只需从元素中获取ID:

var selector = "#" + target.id;

If you want it to be very expressive you could do, or perhaps remove the else block.如果您希望它非常有表现力,您可以这样做,或者删除 else 块。

const attributes = target.getAttributeNames();

let query = target.localName;

for (let i = 0; i < attributes.length; i++) {
  const a = attributes[i];
  const attrValue = target.getAttribute(a);
  if (!attrValue) return;

  if (a === 'id') {
    query += `#${attrValue}`;
  } else if (a === 'class') {
    query += `.${attrValue.split(' ').join('.')}`;
  } else {
    query += `[${a}="${attrValue}"]`;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM