[英]Ignore HTML tags by specified name
I have small function that returns me html path 我有返回我html路径的小函数
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>
. 它运作良好,但是有时候我的代码标签中有<style>
或<title>
。 How do i skip this tags in my javascript function, so it wont count them to my nth-child? 我该如何在我的javascript函数中跳过此标记,以免将其计入第n个孩子?
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.