JS-DOM:基础知识(一)

1.childNodes 属性可返回指定节点的子节点的节点列表(childNodes在firefox会选取空白节点)

<script>

window.onload=function(){

  var oUl=document.getElementById("ul1");

  alert(oUl.childNodes.length);//获取子节点长度:11

}

</script>

<ul id="ul1">
    <li>我是文字1<span>我是span</span></li>
    <li>我是文字2</li>
    <li>我是文字3</li>
    <li>我是文字4</li>
    <li>我是文字5</li>
</ul>

2.children //dom选取页面元素对象的子对象时,children 选取不包括空白节点

<script>

window.onload=function(){

  var oUl=document.getElementById("ul1");

  var aEl=oUl.getElementsByTagName("*");

  alert(oUl.children.length);

}

</script>

<ul id="ul1">
    <li>我是文字1<span>我是span</span></li>
    <li>我是文字2</li>
    <li>我是文字3</li>
    <li>我是文字4</li>
    <li>我是文字5</li>
</ul>

3.创建节点:1、createElement :创建标签节点;2、 createTextNode :创建文本节点;

例一:<script>

window.onload=function(){

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

  var oUl=document.getElementById("ul1");

  var index=0;

  oBtn.onclick=function(){

    index++;

    var oLi=document.createElement("li");

    var oTxt=document.createTextNode(index);

    oLi.appendChild(oTxt);

    oUl.apprendChild(oLi);

  }

}

</script>

<input type="button" value="按钮" id="btn1">
<ul id="ul1">
    <li>0</li>
</ul>

例二:<script>

window.onload=function(){

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

  var oUl=document.getElementById("ul1");

  var index=0;

  oBtn.onclick=function(){

    index++;

    var oLi=document.createElement("li");

    oLi.innerHTML=index;

    oUl.appendChild(oLi);

  }

}

</script>

<input type="button" value="按钮" id="btn1">
<ul id="ul1">
    <li>0</li>
</ul>

4.setAttribute(name,value):设置节点上name属性的值为value;

  getAttribute(name) :获取节点上name属性的值;

<script>

window.onload=function(){

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

  oBtn.onclick=function(){

    oBtn.setAttribute("index","hello");

    alert(oBtn.getAttribute("index"));

  }

}

</script>

<input type="button" value="按钮" index="1" id="btn1">

5、removeAttribute(Name): 删除节点上的name属性

<script>

window.onload=function(){

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

  oBtn.onclick=function(){

    oBtn.removeAttribute("index");

  }

}

</script>

<input type="button" value="按钮" index="1" id="btn1">

6、removeChild() :删除子节点

<script>

function fnFirst(oParent){

  if(oParent.firstElementChild){

    return oParent.firstElementChild;

   }else{

      return oParent.firstChild;

    }  

}

window.onload=function(){

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

  var oUl=document.getElementById("ul1");

  var oSpan=document.getElementById("span1");

  

  oBtn.onclick=function(){

    oUl.children[0].removeChild(oSpan);

  }

}

</script>

<input type="button" value="按钮" id="btn1">
<ul id="ul1">
    <li>我是li1 <span id="span1">我是span</span></li>
    <li>我是li2</li>
    <li>我是li3</li>
    <li>我是li4</li>
</ul>

7、replaceChild(newNode,oldNode): newNode替换节点oldNode

<script>

function fnFirst(oParent){

  if(oParent.firstElementChild){

    return oParent.firstElementChild;

  }else{

    return oParent.firstChild;

  }

}

例子一:window.onload=function(){

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

  var oUl=document.getElementById("ul1");

  oBtn.onclick=function(){

    oUl.replaceChild(oUl.children[3], oUl.children[1]);

  }

}

例子二: window.onload=function(){

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

  var oUl=document.getElementById("ul1");

  oBtn.onclick=function(){

    var oDiv=document.createElement("div");

    oDiv.innerHTML="i am a div";

    oUl.replaceChild(oDiv, oUlchildren[1]);

  }

}

</script>

<input type="button" value="按钮" id="btn1">
<ul id="ul1">
    <li>我是li1</li>
    <li>我是li2</li>
    <li>我是li3</li>
    <li>我是li4</li>
</ul>

8、appendChild(node): 向childNodes末尾插入一个节点node;

<script>

window.onload=function(){

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

  var oUl=document.getElementById("ul1");

  var index=0;

  oBtn.onclick=function(){

    index++;

    var oLi=document.createElement("li");

    oLi.innerHTML=index;

    oUl.appendChild(oLi);

  }

}

</script>

<input type="button" value="按钮" id="btn1">
<ul id="ul1">
    <li>0</li>
</ul>

9、insertBefore(node,targetNode): 向targetNode之前插入一个节点node

<script>

function fnFirst(oParent){

  if(oParent.firstElementChild){

    return oParent.firstElementChild;

  }else{

    return oParent.firstChild;

  }

}

方法一:window.onload=function(){

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

  var oUl=document.getElementById("ul1");

  var index=0;

  oBtn.onclick=function(){

    index++;

    var oLi=document.createElement("li");

    oLi.innerHTML=index;

    oUl.insertBefore(oLi, fnFirst(oUl));

  }

}

方法二:window.onload=function(){

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

  var oUl=document.getElmentById("ul1");

  var index=0;

  oBtn.onclick=function(){

    index++;

    var oLi=document.createElement("li");

    oLi.innerHTML=index;

    if(oUl.children.length>0){

      oUl.insertBefore(oLi,oUl.children[0]);

    }else{

      oUl.appendChild(oLi);

     }

  }

}

</script>

<input type="button" value="按钮" id="btn1">
<ul id="ul1"></ul>

10、nextSibling: 下一个兄弟节点; previousSibling: 上一个兄弟节点

<script>

function getNext(obj){

  if(obj.nextElementSibling){

    return obj.nextElementSibling;

  }else{

    return obj.nextSibling;   

   }

}

function getPrevious(obj){

  if(obj.previousElmentSibling){

    return obj.previousElementSibling;  

  }else{

    return obj.previousSibling;

  }

}

window.onload=function(){

  var oUl=document.getElementById("ul1");

  var oLi=oUl.children[3];

  //alert(oLi.nextSibling.innerHTML);

  //alert(oLi.nextElementSibling.innerHTML);

  //alert(oLi.parentNode.id);

  var oParent=oLi.parentNode;

  alert(getNext(oParent).innerHTML);

}

</script>

<div id="div1">woshidiv1</div>
<ul id="ul1">
<li>我是文字1<span>我是span</span></li>
<li>我是文字2</li>
<li>我是文字3</li>
<li>我是文字4</li>
<li>我是文字5</li>
</ul>
<div id="div2">我是div2</div>

11、获取计算后的样式

(1)、currentStyle : ie所支持的获取非行间样式的方法

(2)、getComputedStyle: 非ie所支持的获取非行间样式的方法

<style type="text/css">
#div1{
    height: 100px;
    width: 100px;
    font-size: 12px;
    background-color: #ccc;
}
</style>
<script type="text/javascript">
function getStyle(obj,sStyle){

if (window.getComputedStyle){
        return getComputedStyle(obj, null)[sStyle];
    }
    else{
        return obj.currentStyle[sStyle];
    }
}

window.onload=function (){

var oDiv=document.getElementById("div1");

oDiv.onclick=function (){

// var fontSize=oDiv.style.fontSize;

// alert(oDiv.currentStyle.fontSize);//在ie下获取计算后的样式
        // alert(getComputedStyle(oDiv, null).fontSize);
        alert(getStyle(oDiv,"fontSize"));

}
}
</script>
</head>
<body>
<div id="div1" style="">123456</div>
</body>

JS-DOM:基础知识(一)

时间: 2024-08-27 15:10:20

JS-DOM:基础知识(一)的相关文章

HTML DOM基础知识

HTML DOM基础知识 一.什么是DOM? 1.HTML DOM 定义了访问和操作HTML文档的标准方法. 2.HTML DOM 把 HTML 文档呈现为带有元素.属性和文本的树结构(节点树). 3.通过 JavaScript,您可以重构整个 HTML 文档.您可以添加.移除.改变或重排页面上的项目.要改变页面的某个东西,JavaScript 就需要获得对 HTML 文档中所有元素进行访问的入口.这个入口,连同对 HTML 元素进行添加.移动.改变或移除的方法和属性,都是通过文档对象模型来获得

js最基础知识回顾

一.html/css 1. 什么是盒子模型? padding+border+width/height 2. float 浮动 (1)浮动的特性 半脱离文档流 行内变成块  共处一行 能设置宽高 同级元素有浮动,必须全部都浮动 父级宽度不够,子集掉下来 文本环绕  顶对齐 (2)清除浮动? clear:both; clearfix: clear:after{display:block; content:''; clear:both;} clear{zoom:1;} overflow:hidden;

javascript中DOM基础知识介绍

1.1.     基本概念 1.1.1.      DOM DOM Document Object Model 文档对象模型 就是把HTML文档模型化,当作对象来处理 DOM提供的一系列属性和方法可以视作一个工具箱,极大地方便了我们对文档的处理. 1.1.2.      内容概念 文档(Document):就是指HTML或者XML文件 节点(Node):HTML文档中的所有内容都可以称之为节点,常见的节点有 元素节点 属性节点 文本节点 注释节点 元素(Element):HTML文档中的标签可以

js的基础知识

一.js的语法 略 二.BOM对象 BOM是浏览器对象模型,可以对浏览器窗口进行访问和操作.BOM是js和浏览器对话的工具. 1.window对象方法 alert() 显示带有一段消息和一个确认按钮的警告框. confirm() 显示带有一段消息以及确认按钮和取消按钮的对话框. prompt() 显示可提示用户输入的对话框. open() 打开一个新的浏览器窗口或查找一个已命名的窗口. close() 关闭浏览器窗口. setInterval() 按照指定的周期(以毫秒计)来调用函数或计算表达式

js的基础知识1

1.什么是JavaScript? 是一门基于对象的客户端脚本语言,用来与web页面进行交互 2.web页面三层结构 结构层     表现层    行为层 3.javascript的组成 ECMAScript    (欧洲计算机制造商协会,不是一门语言而是一个组织,用来规范javascript的语法标准) BOM          (浏览器对象模型) DOM            (文档对象模型) 4.javascript实现 通过script标签对实现 通过外部引入实现 5.javascript

JS DOM基础 操作属性、类、CSS样式

操作属性和类 一.属性节点操作 文本节点内容的获取和修改: 语法:elementNode.attributeName ( 元素节点.属性名)     也可以使用“[ ]” ( 元素节点[属性名]) 注意:一般我们操作属性节点时是不需要获取属性节点的,而是直接通过元素节点获取(/修改)属性节点的值.      特别的,有些属性名称与js关键字或者保留字冲突了,只能用另外的名字: class属性:要写成className(class是关键字). label标签的for属性:写成htmlFor. 通过

JS DOM基础 事件概述 事件流 事件处理方法 添加监听器 事件类型 事件对象 事件委托

一.事件概述 事件是什么? 在我们的生活中,都会接触到事件这样一个概念,它通常通过描述发生这件事的时间.地点.人物,发生了什么来进行概括. 同样的在javascript也有这样的一个的东西------事件. 页面上发生的事件:鼠标移动.点击.滚动等等. 事件描述了页面上发生的事情,通常它有以下三个要素组成: 事件源:触发事件的元素 事件类型:事件的触发方式(例如鼠标点击或键盘点击) 事件处理程序(事件监听器):事件触发后要执行的代码(函数形式) Javascript 使我们可以动态的去操作一个页

三、Node.js中基础知识

1.控制台console对象的方法: 1) console.log(); //显示一行字符串 2) console.info(); //与1)完全相同 3) console.error(); //标准错误输出流的输出 4) console.warn(); //与3)完全相同 5) console.dir(); //查看一个对象中的内容并将其信息输出到控制台 6) console.time(); //标记开始时间 7) console.timeEnd(); //标记结束时间 8) console.

js学习——基础知识

JavaScript                         //这是注释 a = 1;//简单赋值语句 数据类型                         js有动态类型,也就是相同的变量可用作不同类型(python也是) 数字 Number var x = 5;//只有一种数字类型 var x = 5.0; var x = 5e5; var x = 5e-5; 所有数据都以 64 位浮点型数据存储 字符串 String var x = "5"; 布尔 Boolean

js函数基础知识

//return 返回值.function box(){ alert("记得要调用哦!") }box(); //函数本身没有运行功能,需要调用才可以执行. function box(name,job){ alert("姓名:" + name + "工作:" + job); box(); //调用函数时,如果没有输入里面的参数,这种情况则会输出姓名:undefined工作undefiedbox("123","web前端