zoukankan      html  css  js  c++  java
  • js | javascript获取和设置元素的属性

    获取和设置元素的内容:

    var nv = document.getElementById("pid");  

    alert(nv.innerHTML);  

    nv.innerHTML="<a href='http://www.sina.com'>到新浪</a>";    //浏览器会将inneHTML后面的内容作为html来解析

    nv.innerText="World";    //浏览器会将innerText后面的内容作为纯文本来解析

    <input type="button" value="value"onclick="show(this)"></input>

    nv.value="world";  //value是元素的属性值,而innerText和innerHTML是元素开始和结束标签之间的值。

    自定义属性:

    window.onload=function(){

      var oA = document.getElementsByTagName("a");

      alert(oA[0].href);  //只能获取到元素中自带的属性

      alert(oA[0].getAttribute("width"));  //getAttribute() 能获取到元素的所有属性

      oA.setAttribute("tittle","a lot of goods") //建立一个属性,并同时给属性捆绑一个值

      createAttribute:仅建立一个属性
      removeAttribute:删除一个属性
      getAttributeNode:获取一个节点作为对象
      setAttributeNode:建立一个节点
      removeAttributeNode:删除一个节点

    }

    dom操作css:

    window.onload=function(){

       var oBtn1 = document.getElementById("btn1");

      var oDiv = document.getElementsByTagName("div")[0];

      var oA = document.getElementsByTagName("a")[0];

      oBtn1.onclick=function(){

        oDiv.style.width="200px";

        oDiv.className="current";

        oA.style.display="block";  

      }

    }

    this.parentNode.style.display="none";  //操作父级

    this.children[1].style.display="none";  //操作子集

  • 相关阅读:
    洛谷 P2756 飞行员配对方案问题 (二分图匹配)
    HDU 1879 继续畅通工程 (最小生成树)
    POJ 3090 Visible Lattice Points (欧拉函数)
    SPOJ VFMUL
    洛谷 P3803 【模板】多项式乘法(FFT)
    JAVA MyBatis 逆向工程 遇到的坑
    RabbitMQ遇到的坑
    .net webapi action拦截器 统计action耗时
    CEFCharp下载问题
    【进击.NET高级程序员之路】【二】
  • 原文地址:https://www.cnblogs.com/guanzelin/p/8794917.html
Copyright © 2011-2022 走看看