Come posso ottenere tutti gli elementi tr senza l'attributo id?
<tr id="name">...</tr>
<tr>...</tr>
<tr>...</tr>
Grazie
Risposte:
Abbastanza diretto:
//tr[not(@id) and not(@class)]
Questo ti darà tutti gli trelementi privi di entrambi ide classattributi. Se vuoi che tutti gli trelementi manchino di uno dei due, usa orinvece di and:
//tr[not(@id) or not(@class)]
Quando attributi ed elementi vengono utilizzati in questo modo, se l'attributo o l'elemento ha un valore viene trattato come se fosse vero. Se manca, viene trattato come se fosse falso.
Se stai cercando un elemento che abbia una classe ama non abbia una classe b, puoi fare quanto segue.
//*[contains(@class, 'a') and not(contains(@class, 'b'))]
O se vuoi essere sicuro di non abbinare parziale.
//*[contains(concat(' ', normalize-space(@class), ' '), ' some-class ') and
not(contains(concat(' ', normalize-space(@class), ' '), ' another-class '))]
Puoi provare //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 + ']');
}