zoukankan      html  css  js  c++  java
  • javascript动态创建对象

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <script type="text/javascript">
            function btnClick() {
                var div = document.getElementById("divMian");
                var input = document.createElement("input"); //根据标签名创建DOM
                input.type = "button";
                input.value = "动态创建按钮";
                div.appendChild(input);
            }
            function btnClick1() {
                var link = document.getElementById("Link1");
                alert(link.innerHTML);
                alert(link.innerText);
            }
            function createTable() {
                //冒号的前面为Key
                var data = { "百度": "http://www.baidu.com", "新浪": "http://www.sina.com" };
                var div = document.getElementById("table");
    
                for (var Key in data) {
                    var tr = document.createElement("tr");
                    var value = data[Key];
    
                    var td1 = document.createElement("td");
                    td1.innerText = Key;
                    tr.appendChild(td1);
    
                    var td2 = document.createElement("td");
                    td2.innerHTML = "<a href='" + value + "'>" + value + "</a>";
                    tr.appendChild(td2);
    
                    div.appendChild(tr);
                }
    
            }
            function key() {
                var data = { "百度": "http://www.baidu.com", "新浪": "http://www.sina.com" };
                for (var va in data) {
                    alert(va);
                }
            }
        </script>
    </head>
    <body>
        <div id="divMian">
        </div>
        <input type="button" onclick="btnClick()" value="创建按钮" />
        <a href="http://baidu.com" id="Link1">传<font color="red">智</font>播客</a>
        <input type="button" onclick="btnClick1()" value="innerText与innerHTML" />
        <br />
        <table id="table">
        </table>
        <input type="button" value="创建" onclick="createTable()" /><br />
        <input type="button" value="输出key" onclick="key()" />
    </body>
    </html>
  • 相关阅读:
    error in ./src/views/demo/ueditor.vue Module build failed: Error: Cannot find module 'node-sass' Require stack:
    Spring Cloud Stream 定时任务消息延迟队列
    项目结构介绍
    Java面试题
    SpringBoot中用SpringSecurity实现用户登录并返回其拥有哪些角色
    MySQL索引优化
    MySQL中的执行计划explain
    SpringBoot之单体应用
    SpringBoot之SSM多模块应用
    Spring-aop面向切面编程笔记
  • 原文地址:https://www.cnblogs.com/sumg/p/3742763.html
Copyright © 2011-2022 走看看