zoukankan      html  css  js  c++  java
  • Dom之标签属性

    一、标签默认属性的查找与修改

    查找

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>标签操作</title>
    </head>
    <body>
    <!--定义一个div标签和一个a标签-->
    <!--div默认属性:id,class,style,可以通过"."的方式找到,自定义属性则不可以-->
    <div id="container" class="c1" style="font-size: 12px;color: red" sb="'xx">
    <div>武功秘籍</div>
    <div>家常菜谱</div>
    </div>
    <a href="http://www.baidu.com">添加</a>
    <script type="text/javascript">
    var obj = document.getElementById('container');
    console.log(obj.id);
    // class属性对应className值,font-size属性对应fontSize值
    console.log(obj.className);
    console.log(obj.style.fontSize);
    console.log(obj.sb);
    </script>
    </body>
    </html>


    结果:

    container
    c1
    12px
    undefined

      修改

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>标签操作</title>
    </head>
    <body>
        <!--定义一个div标签和一个a标签-->
        <!--div默认属性:id,class,style,可以通过"."的方式找到,自定义属性则不可以-->
        <div id="container" class="c1" style="font-size: 12px;color: red" sb="'xx">
            <div>武功秘籍</div>
            <div>家常菜谱</div>
        </div>
        <a href="http://www.baidu.com">添加</a>
        <script type="text/javascript">
            var obj = document.getElementById('container');
    //        对已列出的默认属性可以修改其值,没有列出的默认属性或自定义属性只能通过setAttribute设置
            obj.id='con';
            obj.className = 'cla_c1';
            obj.style.fontSize='20px';
        </script>
    </body>
    </html>
    

      

    二、标签属性管理的万能神器

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>标签操作</title>
    </head>
    <body>
        <!--定义一个div标签和一个a标签-->
        <!--div默认属性:id,class,style,可以通过"."的方式找到,自定义属性则不可以-->
        <div id='container'  style="font-size: 12px;color: red" sb="'xx">
            <div>武功秘籍</div>
            <div>家常菜谱</div>
        </div>
        <a href="http://www.baidu.com">添加</a>
        <script type="text/javascript">
            var obj = document.getElementById('container');
    //        对于标签默认属性或自定义属性均可设置,且名称定义可见即可得
            obj.setAttribute('class','container');
            obj.setAttribute('kong','kongzhagen');
    
        </script>
    </body>
    </html>
    

      效果图

  • 相关阅读:
    swagger,参数,list,swaggerui测试list<string>类型参数示例
    c# 获取路径的方法详解
    The assembly for System.Buffers could not be loaded;无法加载System.Buffers的程序集
    把.net Core 项目迁移到VS2019 for MAC
    Asp.net Core 3.0 Identity 使用smtp账户确认和密码恢复
    Mui使用jquery并且使用点击跳转新窗口
    运行第一个abp项目VS2015+localDB
    Asp.net MVC+Bootstrap3的悬浮式登录框效果
    MVC配置ckeditor+ckfinder
    EF 数据初始化
  • 原文地址:https://www.cnblogs.com/kongzhagen/p/6160827.html
Copyright © 2011-2022 走看看