Jak mogę uzyskać wszystkie elementy tr bez atrybutu id?
<tr id="name">...</tr>
<tr>...</tr>
<tr>...</tr>
Dzięki
Odpowiedzi:
Całkiem proste:
//tr[not(@id) and not(@class)]
Który daje wszystkie trelementy brakuje zarówno idi classatrybuty. Jeśli chcesz, aby wszystkie trelementy nie posiadały jednego z dwóch, użyj orzamiast and:
//tr[not(@id) or not(@class)]
Gdy atrybuty i elementy są używane w ten sposób, jeśli atrybut lub element ma wartość, jest traktowany tak, jakby był prawdziwy. Jeśli jej brakuje, jest traktowane jako fałszywe.
Jeśli szukasz elementu, który ma klasę, aale jej nie ma b, możesz wykonać następujące czynności.
//*[contains(@class, 'a') and not(contains(@class, 'b'))]
Lub jeśli chcesz mieć pewność, że nie dopasujesz częściowego.
//*[contains(concat(' ', normalize-space(@class), ' '), ' some-class ') and
not(contains(concat(' ', normalize-space(@class), ' '), ' another-class '))]
Możesz spróbować //tr[not(@id)]?
if (elm.hasAttribute('id')) {
//if id - implement here
} else if (elm.hasAttribute('class')) {
//if class - implement here
} else {
for (i = 1, sib = elm.previousSibling; sib; sib = sib.previousSibling) {
if (sib.localName == elm.localName)
i++;
};
segs.unshift(elm.localName.toLowerCase() + '[' + i + ']');
}