zoukankan      html  css  js  c++  java
  • Html5 JumpStart学习笔记5:Dom Interactions

    Module Agenda

    Query the DOM

    Manipulating(操纵) the DOM

    Responding to events

    1、Query the DOM

        getElementById

    var x = document.getElementById("anyID");
    //or
    var x = document.querySelector("#anyID");

        getElementByTagName

        querySelector

        querySelectorAll

    var thing = document.querySelector("#anyID");
    var list = document.querySelectorAll(".item");

    2、Manipulating the DOM

        add | modify | remove

        change style

    var x = document.querySelector("#anyID");
    x.innerText = "changed";
    x.className = "item";
    //or
    x.classList.add("item");

    eg: 每点一下,增加一行

    image

    <div class="dom fragment">
        <header aria-label="Header content" role="banner">
            <button class="win-backbutton" aria-label="Back" disabled type="button"></button>
            <h1 class="titlearea win-type-ellipsis">
                <span id="domTitle" class="pagetitle">DOM</span>
            </h1>
        </header>
        <section id="domContainer" aria-label="Main content" role="main">
            <p id="domButton"Click any content...</p>
        </section>
    <div>
    (function () {
        var container;
        function addItem(item) {
            item.addEventListener("click", function () {
                item.className = "item";
                item.innerText = "item [li clicked]";
                var newItem = document.createElement("li");
                newItem.innerText = "item";
                item.parentElement.appendChild(newItem);
                addItem(newItem);
            });
        }
        WinJS.UI.Pages.define("/pages/dom/dom.html", {
            // This funciton is called whenever a user navigates to this page.It
            // populates the page elements with the app's data.
            ready: function (element, options) {
                //TODO:Initialize the page here.
                var pbutton = document.getElementById("domButton");
                var title = document.getElementById("domTitle");
                container = document.getElementById("domContainer");
                title.addEventListener("click", function () {
                    title.innerText = "DOM [span innerText]";
                });
                pbutton.addEventListener("click", function () {
                    var txt = pbutton.innerText;
                    container.removeChild(pbutton);
                    var btn = document.createElement("button");
                    btn.textContent = txt;
                    container.appendChild(btn);
                    var newDiv = document.createElement("div");
                    newDiv.innerHTML = "<ul><li>item</li></ul>";
                    container.appendChild(newDiv);
                    var domItem = newDiv.querySelector("li");
                    addItem(domItem);
                });
            }
        })
    })

    3、Responding to events

        declarative binding

    <button id="btn" onclick="handler();">Click!</button>
    //JavaScript
    function handler(){ ... }

        programmatic binding

    <button id="btn">Click!</button>
    //JavaScript
    function handler(){ ... }
    var b = document.querySelector("#btn");
    b.addEventListener("click",handler);

            这两种事件的绑定方式当然是推荐使用下面一种啦,行为与表现分离。

  • 相关阅读:
    kafka学习3——单机伪集群(window版)
    (转)在阿里云 CentOS 服务器(ECS)上搭建 nginx + mysql + php-fpm 环境
    渗透测试工具sqlmap基础教程
    编译安装nginx后service nginx start 启动不了
    centos6.5上tomcat的安装
    centos6.5上安装淘宝tfs系统
    解决nagios登录不了的问题
    centos6.5上网络不通解决方法
    redis远程密码连接
    centos7上定时将mysql数据导出并且发送到指定的邮箱
  • 原文地址:https://www.cnblogs.com/jacktag/p/3026098.html
Copyright © 2011-2022 走看看