I have small function that returns me html path
getDomPath = function(el) {
var count, element, path, selector;
count = void 0;
element = void 0;
path = void 0;
selector = void 0;
element = el;
if (!(el instanceof Element)) {
return;
}
path = [];
while (el.nodeType === Node.ELEMENT_NODE && el.id !== "jobs") {
selector = el.nodeName.toLowerCase();
if (el.id) {
selector += "#" + el.id;
} else if (el.className) {
selector += "." + el.className;
} else {
count = $(el).prevAll().length + 1;
selector += ":nth-child(" + count + ")";
}
path.unshift(selector);
el = el.parentNode;
}
return path.join(" > ");
};
It works well, but sometimes i have in my code tags like <style>
or <title>
. How do i skip this tags in my javascript function, so it wont count them to my nth-child?
For ex. i have following code
<table>
<sometag></sometag>
<tr>
<p>My paragraph here</p>
<tr>
</table>
So as result of this function i want to be
table:nth-child(1) > tr:nth-child(1) > p:nth-child(1)
but instead i would get
table:nth-child(1) > tr:nth-child(2) > p:nth-child(1)
because script counts all previous elements
感谢@JasonP,他说我可以按照以下方式进行操作count = $(el).prevAll(':not(sometag)').length + 1;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.