zoukankan      html  css  js  c++  java
  • DOM 之 document 查找元素方法

    DOM 之 document 查找元素方法
    getElementById("idName"); // 始终取得第一个 idName 的元素

    getElementsByTagName("元素标签名") // 返回是一个集合,可用[索引值]来获取相关指定元素或者通过 item(索引值),如

    getElementsByTagName("p")[0] / getElementsByTagName

    ("p").item(1);

    getElementsByName("name"); // 也就是说元素必须带有 name 属性才可以获取,也是一个数组,如 <input type="text" name="name" />

    ===================
    文档写入
    write()/writeln()
    document.write("内容"); / document.writeln("内容");

    ===============
    取得特性
    getAttribute()

    // getAttribute() 用法
    var div = document.getElementById("myDiv");
    alert(div.getAttribute("id"));
    alert(div.getAttribute("class"));
    alert(div.getAttribute("title"));

    ==================

    取得特性
    setAttribute()

    // setAttribute() 用法
    var div = document.getElementById("myDiv");
    alert(div.setAttribute()("id", "myid"));
    alert(div.setAttribute()()("class", "myClass"));
    alert(div.setAttribute()("title", "myTitle"));

    ============

    移除特性
    removeAttribute()

    // removeAttribute() 用法
    var div = document.getElementById("myDiv");
    alert(div.removeAttribute("id"));
    alert(div.removeAttribute("class"));
    alert(div.removeAttribute("title"));

  • 相关阅读:
    [转]IDEA 新建 JSP 项目时
    [转] AForge.NET 图像处理类
    [转] 端口被占用的处理
    [极客大挑战 2019]PHP
    今天鸽了
    [ZJCTF 2019]NiZhuanSiWei
    [极客大挑战 2019]Secret File
    [SUCTF 2019]Pythonginx
    [CISCN2019 华北赛区 Day1 Web2]ikun
    [极客大挑战 2019]EasySQL
  • 原文地址:https://www.cnblogs.com/lin3615/p/3648768.html
Copyright © 2011-2022 走看看