• 09-DOM元素操作


    DOM元素操作

    一、操作元素样式

    通过JS方法获取设置元素属性和属性值

    1. 获取及操作行内样式
    1. 获取元素的样式:DOM.style

      <body><div>div>body>
      <script>
      var box = document.querySelector('div')
      console.log(box.style) //输出的是可以设置的CSS属性
      script>
      
      • 1
      • 2
      • 3
      • 4
      • 5
    2. 当css属性只有一个英文单词的时候,可以借助对象方式进行操作

      元素.style.css属性 = "css属性值"

      box.style.color = 'red'

    3. 当css属性有多个英文单词的时候

      1. 元素.style.css属性值驼峰式写法 = "css属性值"

        box.style.fontSize = "30px"

      2. 元素.style["css属性"] = "css属性值"

        box.style['background-color'] = "pink"

    使用style方式可以给元素赋上样式,这种方式给的样式是行内样式

    2. 获取非行内样式

    获取非行内样式有两种写法

    1. 标准写法:window.getComputedStyle("元素")
    2. 兼容写法:元素.currentStyle.CSS属性
    // 1. 获取非行内样式
    var box = document.querySelector('div')
    console.log(box.style.width) // 输出空,没办法获取到
    console.log(window.getComputedStyle(box).width)
    
    // 2. 兼容写法 由于querySelector() 获取元素结构的时候有兼容问题,所以不推荐使用
    var box = document.getElementsByTagName('div')[0] //集合
    if(window.getComputedStyle){
        var res = window.getComputedStyle(box).width
    }
    else{
        var res = box.currentStyle.width
    }
    console.log(res)
    
    // 3. 封装非行内样式获取的使用函数
    function getStyle(ele,attr){
        if(window.getComputedStyle){
            return window.getComputedStyle(ele)[attr]
        }
        else{
            return ele.currentStyle[attr]
        }
    }
    var box = document.getElementsByTagName('div')[0]
    var res = getStyle(box,'width')
    console.log(res)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    3. 获取表单中的值

    元素.value

    <body>
        <input type="text" name="" id="">
        <select name="" id="">
            <option value="">选择你的城市option>
            <option value="1">深圳option>
            <option value="2">广州option>
            <option value="3">东莞option>
            <option value="4">佛山option>
        select>
        <button >提交button>
    body>
    <script>
        // 获取文本框
        var input = document.querySelector("[type='text']")
        // 获取下拉框
        var select = document.querySelector("select")
        var btn = document.querySelector("button")
        // 获取value值
        btn.onclick = function(){
            console.log(input.value)
            console.log(select.value)
            //获取select对应设置好value
        }
    
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    二、操作元素类名

    可以通过操作元素类型的方法批量操作样式

    1. className: 设置标签中的类名

    ​ 添加:元素.className = '类名'

    ​ 输出:元素.className = ''

    <style>
        .active{
            color:red
        }
    style>
    <body>
    <div>测试文本!!!div>
        <button onclick="add()">点击切换button>
        <button onclick="del()">点击删除button>
    body>
    <script>
        var div = document.querySelector("div")
    // 1. 添加:元素.className = '类名'
        function add(){
            div.className = "active"
        }
    // 2. 删除:元素.className = ''
        function del(){
            div.className = "" 
        }
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    2. classList: 元素本身自带一个属性

    ​ 添加:元素.classList.add('类名')

    ​ 删除:元素.classList.remove('类名')

    ​ 切换:元素.classList.toggle('类名')

    var p = document.querySelector('p')
    // 1. 添加  元素.classList.add('类名')
    p.classList.add('active')
    // 2. 删除  元素.classList.remove('类名')
    p.classList.remove('active')
    // 3. 切换  元素.classList.toggle('类名')
    function fn(){
        p.classList.toggle('active')
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    三、操作元素属性

    1. 原生属性操作

    直接使用属性名进行操作即可

    1. 获取:元素.属性名
    
    2. 设置:元素.属性名 = "属性值"
    
    • 1
    • 2
    • 3

    注意:

    1. 原生属性内有特殊的布尔类型属性
    2. checked、disabled 这种属性获取到的值是布尔值,也可以直接设置布尔值进行操作
    <body>
        <input type="checkbox" checked>
    body>
    <script>
        var input = document.querySelector('input')
        console.log(input.type) // checkbox
        console.log(input.checked) // true
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    案例:密码可视化

    2. 自定义属性操作
    1. 增加  `元素.setAttribute('属性名','属性值')`
    2. 删除 `元素.removeAttribute('属性名')`
    3. 修改 `元素.setAttribute('属性名','属性值')`
    4. 查询 `元素.getAttribute('属性名')`
    
    • 1
    • 2
    • 3
    • 4
    <body>
        <div>测试文本!!!div>
        <button>添加颜色button>
        <button>删除颜色button>
        <button>更换颜色button>
    body>
    <script>
        var box = document.querySelector("div")
        // 增加
        var btn1 = document.querySelector("button:nth-of-type(1)")
        btn1.onclick = function(){
            box.setAttribute("style","color:red")
        }
        // 删除
        var btn2 = document.querySelector("button:nth-of-type(2)")
        btn2.onclick = function(){
            box.removeAttribute("style")
        }
        // 修改
        var btn3 = document.querySelector("button:nth-of-type(3)")
        btn3.onclick = function(){
            box.setAttribute("style","color:green")
        }
        // 查询
        console.log(box.setAttribute('style')) // 没有获取到就会返回NULL
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    3. H5自定义属性的操作

    常见::

    1. data- 开头的表示是H5中自定义属性
    2. data-xxx 后面的内容才是当前属性名
    3. 在等于号后面的才是当前属性的属性值
    4. 自定义会本身携带一个属性:dataset

    语法:自定义的语法

    1. 增加  `元素.dataset.属性名`
    
    2. 删除  `delect 元素.dataset.属性名`
    3. 查询  `元素.dataset.属性名`
    
    • 1
    • 2
    • 3
    • 4
    <p>p>
    
    <script>
        var p = document.querySelector('p')
        // 1. 增
        p.dataset.nianLing = 18
        // 2. 删
        delete p.dateset.nianLing
        // 3. 查
        console.log(p.dataset.nianLing)
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    四、操作元素内容

    JS方法中有三个经常用来操作(插入/替换)元素的内容

    1. DOM元素.innerHTML
    • 可以获取dom结构中的文本 DOM元素.innerHTML
    • 可以替换dom结构中的文本 DOM元素.innerHTML="新文本内容"
    • 修改文本内容时,可以解析标签 DOM元素.innerHTML="新文本内容"
    2. DOM元素.innerText
    • 可以获取dom结构中的文本 DOM元素.innerText
    • 可以替换dom结构中的文本 DOM元素.innerText="新文本内容"
    • 修改文本内容时,不可以解析标签
    3. DOM元素.outerHTML
    • 可以获取dom结构中的文本(含有标签) Dom元素.outerHTML
    • 可以替换dom结构中的文本 Dom元素.outerHTML = "重新设置的文本内容"
    • 可以解析标签,替换时会把原标签一起替换掉(不建议用)
  • 相关阅读:
    阿晨的运维笔记 | CentOS部署Docker
    Linux的常见指令
    C++Day5
    如何设计出优秀的虚拟展厅,设计虚拟展厅有哪些步骤
    asp.net core webapi signalR通信
    Axure绘制数字加减器
    聊聊logback的ThresholdFilter
    Win 下 ncnn 编译运行
    阿里这份Java程序性能优化指南,让你的程序快上200%
    Nginx http.server.location配置项说明
  • 原文地址:https://blog.csdn.net/qq_41570386/article/details/127699728