📜  JavaScript-document.getElementsByTagName()方法

📅  最后修改于: 2020-10-24 08:30:14             🧑  作者: Mango

Javascript-document.getElementsByTagName()方法

document.getElementsByTagName()方法返回指定标签名称的所有元素。

下面给出了getElementsByTagName()方法的语法:

document.getElementsByTagName("name")

在此,名称为必填项。

document.getElementsByTagName()方法的示例

在此示例中,我们将计算文档中使用的段落总数。为此,我们调用了document.getElementsByTagName(“ p”)方法,该方法返回所有段落。


This is a pragraph

Here we are going to count total number of paragraphs by getElementByTagName() method.

Let's see the simple example

上面例子的输出


This is a pragraph
Here we are going to count total number of paragraphs by getElementByTagName() method.
Let’s see the simple example

document.getElementsByTagName()方法的另一个示例

在此示例中,我们将计算文档中使用的h2和h3标签的总数。


This is h2 tag

This is h2 tag

This is h3 tag

This is h3 tag

This is h3 tag

上面例子的输出


This is h2 tag
This is h2 tag
This is h3 tag
This is h3 tag
This is h3 tag


注意:给定示例的输出在此页面上可能会有所不同,因为它将计算本文档中使用的para的总数,h2的总数和h3标记的总数。