zoukankan      html  css  js  c++  java
  • no-jquery 02 DOM

    DOM Manipulation

    Creating Elements

    // IE 5.5+
    document.createElement('div');
    

    Inserting Elements Before & After

    // IE 4+
    document.getElementById('1')
        .insertAdjacentHTML('afterend', '<div id="1.1"></div>');
        
    // IE 4+
    document.getElementById('1')
        .insertAdjacentHTML('beforebegin', '<div id="0.9"></div>');
    

    Inserting Elements As Children

    // IE 4+
    document.getElementById('parent')
        .insertAdjacentHTML('afterbegin', '<div id="newChild"></div>');
    
    // IE 4+
    document.getElementById('parent')
        .insertAdjacentHTML('beforeend', '<div id="newChild"></div>')
    

    Moving Elements

    // IE 5.5+
    document.getElementById('parent')
        .appendChild(document.getElementById('orphan'));
    
    // IE 5.5+
    document.getElementById('parent')
        .insertBefore(document.getElementById('orphan'), document.getElementById('c1'));
    

    Removing Elements

    // IE 5.5+
    document.getElementById('foobar').parentNode
        .removeChild(document.getElementById('foobar'));
    

    Adding & Removing CSS Classes

    // All modern browsers, with the exception of IE9
    document.getElementById('foo').classList.add('bold');
    
    // All browsers
    document.getElementById('foo').className += 'bold';
    
    
    // All modern browsers, with the exception of IE9
    document.getElementById('foo').classList.remove('bold');
    
    // All browsers
    document.getElementById('foo').className = 
        document.getElementById('foo').className.replace(/^bold$/, '');
    

    Adding/Removing/Changing Attributes

    // IE 5.5+
    document.getElementById('foo').setAttribute('role', 'button');
    
    // IE 5.5+
    document.getElementById('foo').removeAttribute('role');
    

    Adding & Changing Text Content

    // IE 5.5+
    document.getElementById('foo').innerHTML = 'Goodbye!';
    
    // IE 5.5+ but NOT Firefox
    document.getElementById('foo').innerText = 'GoodBye!';
    
    // IE 9+
    document.getElementById('foo').textContent = 'Goodbye!';
    

    Adding/Updating Element Styles

    // IE 5.5+
    document.getElementById('note').style.fontWeight = 'bold';
    
  • 相关阅读:
    Hadoop学习笔记——配置文件
    Hive学习笔记——SerDe
    MapReduce中的OutputFormat
    Nginx 转发时的一个坑,运维居然让我背锅!!
    教你用 Netty 实现一个简单的 RPC!
    完整的支付系统整体架构!
    String 拼接一定会走 StringBuilder?
    腾讯牛逼!终于开源了自家的 Tencent JDK——Kona!!
    Java 数组转 List 的 3 种方式,哪种性能最牛?
    Spring Boot 2.4.0 发布,配置文件重大调整,不要乱升级!!
  • 原文地址:https://www.cnblogs.com/jinkspeng/p/4478203.html
Copyright © 2011-2022 走看看