JavaScript获取指定Class下的所有标签

以下代码是  获取class="lqwvje"下所有的 img标签
    //原创来自:http://www.luofenming.com/show.aspx?id=ART2019031300001
    <div class="lqwvje">
        <img src="http://www.luofenming.com/upload/image/2019/3/9/20190309205613_7873.jpg" />
        <img src="http://www.luofenming.com/upload/image/2019/3/9/20190309205613_7873.jpg" />
        <img src="http://www.luofenming.com/upload/image/2019/3/9/20190309205613_7873.jpg" />
    </div>
    <div class="lqwvje22">
        <img src="http://www.luofenming.com/upload/image/2019/3/9/20190309205613_7873.jpg" />
        <img src="http://www.luofenming.com/upload/image/2019/3/9/20190309205613_7873.jpg" />
        <img src="http://www.luofenming.com/upload/image/2019/3/9/20190309205613_7873.jpg" width="400" />
    </div>
    //方法一
    <script type="text/javascript">
        var images = [];
        var divs = document.querySelectorAll(".lqwvje img");
        for (var i = 0; i < divs.length; i++) {
                images.push(divs[i]);//这是获得 class="lqwvje" 下的所有img标签
        }
    </script>
    //方法二
    <script type="text/javascript">
        var images = [];
        var divs = document.getElementsByClassName("lqwvje");
        for (var i = 0; i < divs.length; i++) {
            var imgs = divs[i].getElementsByTagName("img");
            for (var j = 0; j < imgs.length; j++) {
                images.push(imgs[j]);//这是获得 class="lqwvje" 下的所有img标签
            }
        }
    </script>
同样 也可以延申到  获取任何DIV下所有的标签 等等

评论

  1. 112.97.60.*2019-03-13 20:14
    你是不是应该要感谢我!