【问题标题】:Is there a way of combining HTML tags like you can with style tabs?有没有一种方法可以将 HTML 标签与样式选项卡结合起来?
【发布时间】:2023-01-26 04:27:40
【问题描述】:

在 Javascript 中有没有一种方法可以像使用样式标签那样组合 HTML 标签?

示例 HTML 文档:

<!DOCTYPE html>
<html>
<body>

<div>
  <p>I am paragraph one.</p>
  <p>I am paragraph two.</p>
  <p>I am paragraph three.</p>
</div>

</body>
</html>

就像是: document.body.getElementsByTagNames("div p")

我编造了 getElementsByTagNames。

div p {颜色:绿色}

您可以使用如下代码找到文档中的所有标签:

var divisions = document.body.getElementsByTagName("div");

你可以找到所有

第一个标签中的标签,代码如下:

var paragraphs = divisions[0].getElementsByTagname("p");

https://www.oreilly.com/library/view/javascript-the-definitive/0596101996/re285.html

【问题讨论】:

    标签: javascript


    【解决方案1】:

    使用document.querySelectorAll

    document.querySelectorAll('div p').forEach(p => {
      console.log(p.outerHTML);
      p.style.color = 'green';
    });
    <div>
      <p>I am paragraph one.</p>
      <p>I am paragraph two.</p>
      <p>I am paragraph three.</p>
    </div>

    【讨论】:

      猜你喜欢
      • 2023-01-01
      • 2020-06-27
      • 1970-01-01
      • 1970-01-01
      • 2012-08-19
      • 2011-08-24
      • 2015-04-05
      • 1970-01-01
      • 2021-02-21
      相关资源
      最近更新 更多