正如Barmar提到的,使用$$()用CSS Child Selector(虽然基本Descendant Selector将工作以及)像'.price-excl-tax > .price'
。
请看下面的例子。请注意,它将Event.observe()用于dom:loaded事件(PrototypeJS特有),以确保DOM在查询之前已加载。另请注意,innerHTML属性用于获取price元素的内容,但如果没有嵌套的HTML节点,也可以使用.textContent。
document.observe("dom:loaded", function() {
var priceContainers = $$('.price-excl-tax > .price');
if (priceContainers.length) { //Greater than 0
console.log("Base price is: " + priceContainers[0].innerHTML);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/prototype/1.7.3/prototype.js"></script>
<table>
<tr>
<td class="a-right">
<span class="price-excl-tax">
<span class="price">$299.00</span>
</span>
<br>
</td>
</tr>
</table>
另一种方法将是使用Element.select()。例如:
var priceExclTaxContainers = $$('.price-excl-tax');
if (priceExclTaxContainers.length) { //not empty
var priceContainers = priceExclTaxContainers.select('.price');
if (priceContainers.length) {
//utilize priceContainers[0].innerHTML
}
}
您需要'''在类名的开头。您应该可以使用$$('。price-excl-tax .price')'来匹配类中的类。就像CSS选择器一样。 – Barmar
这实际上是一个关于[后代](https://developer.mozilla.org/en-US/docs/Web/CSS/Descendant_selectors)或[child](https://developer.mozilla.org/en)的CSS问题-US/docs/Web/CSS/Child_selectors)选择器,与JavaScript很少有关。 –
我不同意它与JavaScript没有多大关系。通常我会说按类或ID选择。在这种特殊的风格,它没有如此添加。表明上课会有意义。感谢Barmar,我现在意识到这一点,而不是假设。 – camdixon