Ale*_*yne 45 javascript jquery jquery-selectors
在伪代码中,这就是我想要的.
var selector = $(this).cssSelectorAsString(); // Made up method...
// selector is now something like: "html>body>ul>li>img[3]"
var element = $(selector);
Run Code Online (Sandbox Code Playgroud)
原因是我需要将其传递给外部环境,其中字符串是我交换数据的唯一方式.然后,此外部环境需要发回结果以及要更新的元素.所以我需要能够为页面上的每个元素序列化一个唯一的CSS选择器.
我注意到jquery有一个selector方法,但它似乎不适用于此上下文.它仅在使用选择器创建对象时才有效.如果使用HTML节点对象创建对象,则它不起作用.
Bli*_*ixt 54
我现在看到一个插件存在(我也想到了同名),但这里只是我编写的一些快速JavaScript.它不需要考虑元素的id或类 - 只有结构(并添加:eq(x)节点名称不明确的地方).
jQuery.fn.getPath = function () {
if (this.length != 1) throw 'Requires one element.';
var path, node = this;
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;
};
Run Code Online (Sandbox Code Playgroud)
jQuery-GetPath是一个很好的起点:它会给你项目的祖先,像这样:
var path = $('#foo').getPath();
// e.g., "html > body > div#bar > ul#abc.def.ghi > li#foo"
Run Code Online (Sandbox Code Playgroud)
这是Blixt的一个版本在IE中的作用:
jQuery.fn.getPath = function () {
if (this.length != 1) throw 'Requires one element.';
var path, node = this;
while (node.length) {
var realNode = node[0];
var name = (
// IE9 and non-IE
realNode.localName ||
// IE <= 8
realNode.tagName ||
realNode.nodeName
);
// on IE8, nodeName is '#document' at the top level, but we don't need that
if (!name || name == '#document') break;
name = name.toLowerCase();
if (realNode.id) {
// As soon as an id is found, there's no need to specify more.
return name + '#' + realNode.id + (path ? '>' + path : '');
} else if (realNode.className) {
name += '.' + realNode.className.split(/\s+/).join('.');
}
var parent = node.parent(), siblings = parent.children(name);
if (siblings.length > 1) name += ':eq(' + siblings.index(node) + ')';
path = name + (path ? '>' + path : '');
node = parent;
}
return path;
};
Run Code Online (Sandbox Code Playgroud)
我只是想分享我的版本,因为它很清楚.我在所有常见浏览器中测试了这个脚本,它就像老板一样工作.
jQuery.fn.getPath = function () {
var current = $(this);
var path = new Array();
var realpath = "BODY";
while ($(current).prop("tagName") != "BODY") {
var index = $(current).parent().find($(current).prop("tagName")).index($(current));
var name = $(current).prop("tagName");
var selector = " " + name + ":eq(" + index + ") ";
path.push(selector);
current = $(current).parent();
}
while (path.length != 0) {
realpath += path.pop();
}
return realpath;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
32489 次 |
| 最近记录: |