簡體   English   中英

忽略指定名稱的HTML標記

[英]Ignore HTML tags by specified name

我有返回我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(" > ");
};

它運作良好,但是有時候我的代碼標簽中有<style><title> 我該如何在我的javascript函數中跳過此標記,以免將其計入第n個孩子?

對於前。 我有以下代碼

<table>
<sometag></sometag>
<tr>
<p>My paragraph here</p>
<tr>
</table>

因此,由於這個功能,我想成為

table:nth-child(1) > tr:nth-child(1) > p:nth-child(1)

但是我會得到

table:nth-child(1) > tr:nth-child(2) > p:nth-child(1)

因為腳本會計算所有先前的元素

感謝@JasonP,他說我可以按照以下方式進行操作count = $(el).prevAll(':not(sometag)').length + 1;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM