Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 选定元素在html dom中的路径是什么?_Jquery_Html_Dom_Path - Fatal编程技术网

Jquery 选定元素在html dom中的路径是什么?

Jquery 选定元素在html dom中的路径是什么?,jquery,html,dom,path,Jquery,Html,Dom,Path,我正在尝试获取特定对象在html dom中的路径。 使用javascript或jquery(当我选择/悬停在对象上时) 我事先不知道结构 例如: <html> <body> <div> <div> Not selected </div> <div> Selected

我正在尝试获取特定对象在html dom中的路径。
使用javascript或jquery(当我选择/悬停在对象上时)

我事先不知道结构

例如:

<html>
    <body>
        <div>
            <div>
                Not selected
            </div>
            <div>
                Selected Text
            </div>
        </div>
    </body>
</html>
任何帮助都将不胜感激

我已经看过了,但它没有给出关于元素索引的指示(在我的示例中是
DIV[1]
中方括号-1中的值)

顺便问一下,这个路径有“技术”名称吗?
我见过XPath这个术语,但不确定这就是它

快乐地享受生活。

$(“html>div”)[0]。查找(“div”)[1
]

XPath在Jquery中不再可用。

如果需要Xpath,只需在FF或Chrome下使用Firebug即可

关于选择任何元素(选项):

要使div悬停,请执行以下操作:

$("div:hover")
或复选框:

$("checkbox:selected")
现在让我们举一个例子:

<div>
   <div class='again'>
      <select id='select1'>
         <option val='1'>
         <option val='2'>
      </select>


      <select id='select2'>
         <option val='1'>
         <option val='2'>
      </select>
   </div>
</div>

正在获取悬停的div:

('.secondDIv').hover(function(){
},function(){});

etc etc

以下函数使用jQuery执行您想要的操作:

function getElementPath(element)
{
    return "//" + $(element).parents().andSelf().map(function() {
        var $this = $(this);
        var tagName = this.nodeName;
        if ($this.siblings(tagName).length > 0) {
            tagName += "[" + $this.prevAll(tagName).length + "]";
        }
        return tagName;
    }).get().join("/").toUpperCase();
}
您可以这样使用它:

$(document).ready(function() {
    $("div").click(function(event) {
        event.stopPropagation();
        window.alert(getElementPath(this));
    });
});

你可以试试。

@user544287我不确定你是否理解我的问题。我正在尝试获取被选中/悬停的任何元素的路径,不仅仅是我给出的示例。@Julian,不客气:)我刚刚意识到它不会向没有同级的元素添加
[0]
索引,尽管您的示例有,但我想这可以被视为一个功能,不是虫子:)@Fredeeic Hamidi-这是一个很好的特性;-)酷,我的搜索到此结束,谢谢
$('.again >select')[0].find("option:selected")
('.secondDIv').hover(function(){
},function(){});
function getElementPath(element)
{
    return "//" + $(element).parents().andSelf().map(function() {
        var $this = $(this);
        var tagName = this.nodeName;
        if ($this.siblings(tagName).length > 0) {
            tagName += "[" + $this.prevAll(tagName).length + "]";
        }
        return tagName;
    }).get().join("/").toUpperCase();
}
$(document).ready(function() {
    $("div").click(function(event) {
        event.stopPropagation();
        window.alert(getElementPath(this));
    });
});