또 다른 방법은 DOM 트리를 방황하고 저장하고 다시 선택으로 나중에 사용할 수있는 요소의 경로를 만들 수 있습니다, t하지만이 도움이 될 생각 모자는 방탄하지 않을 수도 있지만, 아마도 당신이 시작할 수있는 지점 일 것입니다.
편집 : 사용할 수
그냥 JSBin의 예를 방문하고 두 번 document
를 클릭하면 지금은 ID를 반환 코멘트에있는 당신의 제안에 답을 업데이트했습니다. 만 강조됩니다 것을 알 ..
jQuery.fn.getPath = function() {
if (this.length != 1) throw 'Requires one element.';
var path, node = this;
if (node[0].id) return "#" + node[0].id;
while (node.length) {
var realNode = node[0],
name = realNode.localName;
if (!name) break;
name = name.toLowerCase();
var parent = node.parent();
var siblings = parent.children(name);
if (siblings.length > 1) {
name += ':eq(' + siblings.index(realNode) + ')';
}
path = name + (path ? '>' + path : '');
node = parent;
}
return path;
};
var sel;
$(document)
.click(function (e, a) {
if (!sel) {
sel = $("#comment-21702402")
.getPath();
alert("Path is: " + sel + ", hiding the Element -> Click again to highlight");
} else {
$(sel)
.css("background-color", "yellow");
}
});
당신은 당신이 ID에 관심이있는 사람을 제공하지 못할 이유
? 그냥 궁금한가? 페이지의 모든 단일 개체에 관심이 있습니까? – mplungjan
http://stackoverflow.com/questions/5442767/returning-the-full-path-to-an-element – mplungjan
@mplungjan 페이지에있는 모든 클릭 된 요소에 대한 선택기를 저장하려는 경우 :)이 스크립트는 많은 페이지에 포함되어 있습니다. – Cristy