zoukankan      html  css  js  c++  java
  • DOM属性及创建标签

                    样式操作:
                        className
                        classList
                            classList.add
                            classList.remove
                            
                        <style>
                            .c1{
                                font-size:15px
                            }
                        </style>    
                        <div class='c1 c2'></div>
                        obj.style.font-size ='16px';
                    属性操作:
                        obj.setAttribute('xxxx','liunx')#添加属性
                        obj.removeAttribute('value')#移除属性
                        obj.attributes#获取所有属性
                    
                    创建标签,并添加到HTML中:
                        a.字符串形式
                        b.对象的方式
                            document.createElement('input');

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <input type="button" onclick="AddEle1();value='+'">
        <input type="button" onclick="AddEle2();value='+'">
    
        <div id="i1">
            <input type="text">
            <hr/>
        </div>
    
        <script>
            function AddEle1() {
                //创建一个标签
                //将标签添加到i1里面
                var tag= document.createElement('input');
                tag.setAttribute('type','text')
                tag.style.fontSize='22px'
                tag.style.color='red'
                // document.getElementById('i1').insertAdjacentHTML("beforeEnd",tag);
                document.getElementById('i1').appendChild(tag);
    
            }
            function AddEle2() {
                //创建一个标签
                //将标签添加到i1里面
                var tag="<input type='text'/><hr/>";
           //注意 第一个参数只能是 beforeEnd afterBegin beforeBegin afterEnd document.getElementById(
    'i1').insertAdjacentHTML("beforeEnd",tag); } </script> </body> </html>
  • 相关阅读:
    使用JS实现复制粘贴功能
    前端向后端发送请求(FormData),你们不要吐槽我,有的时候我也不想写注释
    最全面的数组去重详细解析
    查找字符串数组中的最长公共前缀
    最简单的让多行表格滚动方法
    送给vue初学者的 vue.js技巧
    git 和码云的上传文件代码操作
    常用模块 二
    深拷贝与浅拷贝
    常用模块升级
  • 原文地址:https://www.cnblogs.com/anhao-world/p/14282945.html
Copyright © 2011-2022 走看看