[英]How to get element whose parents child is element x? PHP Simple HTML DOM Parser
So for example I have a HTML tree like this: 例如,我有一个这样的HTML树:
<section class="product"> <div> <div class="p-image"> <img alt="Product name" src="path/to/image.jpg"> </div> <div class="p-content"> <h3>Product name</h3> </div> <div class="p-info"> <div class="new-price"> <span>400 €</span> </div> </div> </section>
So I want to get the content of span element whose parent (div) has a child element (img) with a specific alt attribute. 因此,我想获取span元素的内容,该元素的父元素(div)具有带有特定alt属性的子元素(img)。 I know how to select an element by its attributes, but I haven't found any solution to selecting an element by it's parent's child.
我知道如何通过元素的属性选择元素,但是我还没有找到通过其父元素的孩子选择元素的任何解决方案。
I hope my explanation was understandable. 我希望我的解释是可以理解的。 Thank you.
谢谢。
在jQuery中,您可以使用$(selector).parent()
获得元素的父元素,并使用$(img alt="x")
获得alt属性等于x
的img标签。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.