【问题标题】:How to count the number of anchor tags within a div using javascript?如何使用 javascript 计算 div 中锚标记的数量?
【发布时间】:2009-07-06 11:08:58
【问题描述】:

我想知道如何计算div 元素中存在的锚标记的数量。 例如:

<div>
<a href="1" >1</a>
<a href="2" >2</a>
<a href="3" >3</a>
<a href="4" >4</a>
</div>

有多少&lt;a&gt;标签?

【问题讨论】:

    标签: javascript


    【解决方案1】:
    theDivElement.getElementsByTagName('a').length
    

    【讨论】:

      【解决方案2】:

      使用HTML DOM getElementsByTagName() 获取对象下的所有“a”标签。
      要获得 div 你最好给它一个 ID 然后使用 getElementsByTagName.

      var anchors = document.getElementById("thediv").getElementsByTagName("a");
      alert("The Div has " + anchors.length + " links in it");
      <div id="thediv">
        <a href="#">link 1</a>
        <a href="#">link 2</a>
        <a href="#">link 3</a>
      </div>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-29
        • 1970-01-01
        相关资源
        最近更新 更多