Javascript 关于DOM方法 getElementsByClassName、getElementsByTagName、getElementById、setAttribute、getAttrib...

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="utf-8" />
    <title> 5 DOM Function</title>
    <style type="text/css">
        body{
            color:white;
            background-color: black;
        }
        p{
            color: yellow;
            font-family: "arial", sans-serif;
            font-size: 1.2em;
        }
        #purchases{
            border:1px solid white;
            background-color: #333;
            color:#ccc;
            padding: 1em;
        }
        /* Although id can only be used once, it can also set CSS for other elements which are included in particulur element */
        #purchases li{
            font-weight: bold;
        }
    </style>
</head>
<body>
<!-- A simple example -->
    <h1> What to buy </h1>
    <p title="a gentle reminder"> Don't forget to buy this staff. </p>
    <ul id="purchases">
        <li> A tin of beans </li>
        <li class="sale"> Cheese </li>
        <li class="sale important"> Milk </li>
    </ul>

<!-- getElementById, getElementByTagName, getElementsByClassName, getAttribute, setAttribute -->
<script type="text/javascript">
    /* Let's explain what is the DOM's meaning:
        D : means document;
        O : means objects, there are three types of objects in javascript==>
            user-defined object: the object created by your ownself;
            native object: thw object created by javascript itself, like Array, Math, Date and so on;
            host object: the object provided by web browser, the basic object of host object is window object. And the properties & functions of window object are called "BOM" which I think call it "WOM" is more suitable. BOM provides window.blur & window.open functions.
        M : means "Model" or "Map" that standards the forms of some things.
    */

    /* node : a connection point in the network. The document is made up of nodes. It includes three types => element node; text node; attribute node.
        element node : the html's tags, like <html> <body> and so on;
        text node : just like the contents of <p> =>"XXXXXXX", it is often included in element node, but not every element node has text node;
        attribute node : description of elements, like almost every element node has an attribute node "title", for example, <p> has title="a gentle reminder".
    */

    /* obtain elements : there are three functions can obtain element nodes => via id, tag name, class name;*/

    console.log(typeof document.getElementById("purchases"));
    /* It will show "object" in browser */

    console.log(document.getElementsByTagName("li").length);
    /* this function returns an array, and every element in this array is an object. You can try follows to verify: */
    for(var i = 0;i < document.getElementsByTagName("li").length;i++)
    {
        console.log(typeof document.getElementsByTagName("li")[i]);
    }
    /* The most important thing is to be careful the function's spelling .
        If you want to know how many nodes in a document, you can use this :
        console.log(document.getElementsByTagName("*").length);
        you can also combine getElementsByTagName and getElementById,like follows:
        you will know how many lists the id "purchses" includes.
    */
    var shopping = document.getElementById("purchases");
    var items = shopping.getElementsByTagName("*");
    console.log(items.length);
    for(var i = 0;i < items.length;i++)
    {
        console.log(typeof items[i]);
    }

    /* getElementsByClassName: it also returns an array. */
    var sales = shopping.getElementsByClassName("sale");
    console.log(sales);    
    
    function getElementsByClassName(node, classname){
        if(node.getElementsByClassName)
        {
            //console.log("Yes!");
            return node.getElementsByClassName(classname);
        }else{
            var results = new Array();
            var elems = node.getElementsByTagName("*");
            for(var i = 0;i < elems.length;i++)
            {
                if(elems[i],className.indexOf(classname)!=-1)
                {
                    results[results.length] = elems[i];
                }
            }
            return results;
        }
    }

    var sales2 = getElementsByClassName(shopping, "sale");
    console.log(sales2);


    /* getAttribute(): object.getAttribute(attribute); you can use this function to look for one particular object's attribute. */
    var paras = document.getElementsByTagName("p");
    for(var i = 0;i < paras.length;i++)
    {
        console.log(paras[i].getAttribute("title"));
    }

    /* setAttribute(): object.setAttribute(attribute, value); you can use this function to change or set object's attribute. */
    console.log(shopping.getAttribute("title"));
    shopping.setAttribute("title", "a list of goods");
    console.log(shopping.getAttribute("title"));
    /* It is noteworthy that making changes to the document via function setAttribute() makes no difference when you view source using browser's select "view source". It because of  DOM's operating mode : static content will be first loaded, dynamic refresh again, and dynamic refresh does not affect the static content of documents.
    We can see this function is very wonderful clearly!
    */
</script>
</body>
</html>

o(* ̄▽ ̄*)o,代码里已经说得很清楚了,代码还包括了一些例子,下面放出例子的输出结果:

⊙0⊙,可以将代码复制到你的编辑器里查看。

转载于:https://www.cnblogs.com/yojiaku/p/5753525.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值