• 访问文档中的所有元素


    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>DOM Example</title>
      <script type="text/JavaScript" src="exampleFindElements.js">
      </script>
    </head>
    <body>
      <h1>Heading</h1>
      <p>Paragraph</p>
      <h2>Subheading</h2>
      <ul id="eventsList">
        <li>List 1</li>
        <li>List 2</li>
        <li><a href="http://www.baidu.com">Linked List Item</a></li>
        <li>List 4</li>
      </ul>
      <p>Paragraph</p>
      <p>Paragraph</p>
    </body>
    </html>

    exampleFindElements.js:

    function findElements()
    {
    var listElements=document.getElementsByTagName('li');
    var paragraphs=document.getElementsByTagName('p');
    var msg='This document contains '+listElements.length+' list items '
    msg+=' and '+paragraphs.length+' paragraphs.';
    //alert(msg);
    //Get the first paragraph
    var firstpara=document.getElementsByTagName('p')[0];
    //Get the second list item
    var secondListItem=document.getElementsByTagName('li')[1];
    //Get last list item
    var lastListItem=listElements[listElements.length-1];
    //Get son Item
    var targetLink=document.getElementsByTagName('li')[2].getElementsByTagName('a')[0];
    //Get by id
    var events=document.getElementById('eventsList');

    //Set the value of node
    document.getElementsByTagName('p')[0].firstChild.nodeValue='Hello woorld!';
    alert(firstpara);
    alert(secondListItem);
    alert(lastListItem);
    alert(targetLink);
    alert(events);
    }


    //Get all Chilren node
    function myDOMinspector()
    {
        var DOMstring='';
        if(!document.getElementById||!document.createTextNode){return;}
        var demoList=document.getElementById('eventsList');
        if(!demoList){return;}
        if(demoList.hasChildNodes())
        {
            var ch=demoList.childNodes;
            for(var i=0;i<ch.length;i++)
            {
                DOMstring+=ch[i].firstChild.nodeValue+' '+ch[i].nodeName+' ';
            }
            alert(DOMstring);
        }
    }

    window.onload=findElements;
    window.onload=myDOMinspector;

    imageimageimageimageimage

    image

     

  • 相关阅读:
    CRB and His Birthday(2015多校)
    B. Bear and Three Musketeers
    Uva657
    cas服务端改造
    有用的maven插件
    maven管理的非标准目录
    struts2中的action交由spring管理
    数据库分库
    linux内核系列之二_资源
    linux内核系列之一_工具
  • 原文地址:https://www.cnblogs.com/vonk/p/3976574.html
Copyright © 2020-2023  润新知