goquery get all <p> tags from just one selector

huangapple go评论127阅读模式
英文:

goquery get all <p> tags from just one selector

问题

我有以下示例

  1. <div class="myClass">
  2. <div>
  3. <div>
  4. <p>1</p>
  5. </div>
  6. </div>
  7. <p>2</p>
  8. <div>
  9. </div>
  10. <p>3</p>
  11. </div>

如何使用goquery或CSS选择器(或两者)提取3个p标签?
也许在这种情况下选择器将是"div.myClass"。

英文:

I have the following example

  1. &lt;div class=&quot;myClass&quot;&gt;
  2. &lt;div&gt;
  3. &lt;div&gt;
  4. &lt;p&gt;1&lt;/p&gt;
  5. &lt;/div&gt;
  6. &lt;/div&gt;
  7. &lt;p&gt;2&lt;/p&gt;
  8. &lt;div&gt;
  9. &lt;/div&gt;
  10. &lt;p&gt;3&lt;/p&gt;
  11. &lt;/div&gt;

How can I extract the 3 p tags using goquery or CSS selectors (or both)
Maybe in this case the selector would be "div.myClass"

答案1

得分: 2

你可以使用以下选择器选择.myClass的所有子元素:

  1. .myClass p {
  2. /* 选择所有从.myClass继承的p元素 */
  3. }

关于后代选择器的指南可以在这里找到。

英文:

You can select all child elements of .myClass with a selector like this:

  1. .myClass p {
  2. /* Selects all p elements that descend from .myClass */
  3. }

A guide about descendant selectors can be found here.

答案2

得分: 1

只需使用 p {/* 选择所有 p 元素 */}

英文:

just p {/* Selects all p elements */}

huangapple
  • 本文由 发表于 2022年11月2日 00:36:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/74279024.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定