zoukankan      html  css  js  c++  java
  • js 获取元素的几种方法

    获取 元素/节点/标签 的几点方法

    getElementById

    getElementsByTagName

    getElementsByName

    getByClass √               // 现在通过class 也可以查找节点

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>getByClasss</title>
    <script type="text/javascript">
    function getByClass(oParent, aClass)
    {
        var aEle=oParent.getElementsByTagName('*');
        var arr=[];
        for(var i=0;i<aEle.length;i++)
        {
            if(aEle[i].className==aClass)
            {
                arr.push(aEle[i]);
            }
        }
        return arr;
    }
    window.onload=function()
    {
        var oUl=document.getElementById('ul1');
        var aBox=getByClass(oUl, 'box');
        for(var i=0;i<aBox.length;i++)
        {
            aBox[i].style.background="red";
        }
    }
    </script>
    </head>
    
    <body>
    <ul id="ul1">
        <li></li>
        <li class="box">a</li>
        <li></li>
        <li></li>
        <li class="box">a</li>
        <li></li>
    </ul>
    </body>
    </html>
    Hello Javascript!
  • 相关阅读:
    poj 2104(线段树)
    poj 1962(并查集+带权更新)
    hdu 2818(并查集,带权更新)
    hdu 1856
    hdu 3172
    hdu 1325(并查集)
    hdu 5023
    pku 2777(经典线段树染色问题)
    hdu 1671(字典树判断前缀)
    hdu 1247 (字典树入门)
  • 原文地址:https://www.cnblogs.com/chenyajie/p/2932288.html
Copyright © 2011-2022 走看看