获取选择器的元素路径

Owe*_*wen 7 jquery xpath dom element

遇到麻烦,基本上试图创建一个可以用作选择器的变量.例如

$('a').click(function(){
   var selector = $(this).dompath();
});
Run Code Online (Sandbox Code Playgroud)

HTML:

html
    body
        div
            div /div
        /div
       ul
         li
         li
       /ul
    div
        ul
           li
           li
           li hello world
        /ul
   /div
   body
html
Run Code Online (Sandbox Code Playgroud)

这将返回类似的东西

path = html body div ul li:contains('hello world')
Run Code Online (Sandbox Code Playgroud)

然后我可以在选择器中使用它来选择这个div,所以如果我喜欢

$(path).text() would return "hello world"
Run Code Online (Sandbox Code Playgroud)

非常感谢!

jce*_*ern 14

也许是这样的:

function dompath( element )
{
    var path = '';
    for ( ; element && element.nodeType == 1; element = element.parentNode )
    {
        var inner = $(element).children().length == 0 ? $(element).text() : '';
        var eleSelector = element.tagName.toLowerCase() + 
           ((inner.length > 0) ? ':contains(\'' + inner + '\')' : '');
        path = ' ' + eleSelector + path;
    }
    return path;
}
Run Code Online (Sandbox Code Playgroud)

这修改了另一个问题的方法,:contains()只有当标签没有子标签时才通过运算符添加标签的全文内容.

我用这种方法测试过:

$(document).ready(function(){
    $('#p').click(function() {
      console.log(dompath(this));
    });
});
Run Code Online (Sandbox Code Playgroud)

反对这个:

<html>
    <body>
        <div>
            <div> </div>
        </div>
       <ul>
         <li></li>
         <li></li>
       </ul>
       <div>
         <ul>
           <li id="p">hi</li>
           <li></li>
           <li id="p2">hello world</li>
        </ul>
       </div>
   <body>
<html>
Run Code Online (Sandbox Code Playgroud)

点击p的结果然后输出为:

html body div ul li:contains('hi')


ssh*_*how 5

@jcern神奇代码的修改版本。

特征:

  • 如果元素具有ID:仅显示 #elementId
  • 如果不存在ID:显示 element.className
  • 如果没有一流的存在:显示element与它的innerHtml附加(如果有的话)
  • 跳过<body><html>元素以缩短输出
  • 不依赖jQuery
function dompath(element) {
    var path = '',
    i, innerText, tag, selector, classes;

    for (i = 0; element && element.nodeType == 1; element = element.parentNode, i++) {
        innerText = element.childNodes.length === 0 ? element.innerHTML : '';
        tag = element.tagName.toLowerCase();
        classes = element.className;

        // Skip <html> and <body> tags
        if (tag === "html" || tag === "body")
            continue;

        if (element.id !== '') {
            // If element has an ID, use only the ID of the element
            selector = '#' + element.id;

            // To use this with jQuery, return a path once we have an ID
            // as it's no need to look for more parents afterwards.
            //return selector + ' ' + path;
        } else if (classes.length > 0) {
            // If element has classes, use the element tag with the class names appended
            selector = tag + '.' + classes.replace(/ /g , ".");
        } else {
            // If element has neither, print tag with containing text appended (if any)
            selector = tag + ((innerText.length > 0) ? ":contains('" + innerText + "')" : "");
        }

        path = ' ' + selector + path;
    }
    return path;
}
Run Code Online (Sandbox Code Playgroud)