对元素的操作

1、定位 

var a = document.getElementByIt( "id" )

2、同辈元素 

  var b = a.nextSibling;            // 找 a 的下一个同辈元素,

  var b = a.previousSibling;    // 找 a 的上一个同辈元素,

<html >
<head>
<title></title>
<style type ="text/css">
.div1{
width:100px;
height:100px;
background-color:red;
margin-right:10px;
font-size:35px;
color:white;
float:left ;
}
</style>
</head>
<body>
<div class="div1"></div><div class="div1" id="div2"></div><div class="div1"></div>
</body>
</html>
<script type="text/javascript" >
var odiv2 = document.getElementById("div2");
odiv2.onclick = function () {
alert(odiv2.previousSibling.innerText);
}
</script>

中间隔一个取上一个

  odiv2.previousSibling.previousSibling.innerText

  空格和换行也算一个元素,不管多少空格都算一个,不管多少换行都算一个

<html >
<head>
<title></title>
<style type="text/css">
.div1 {
width: 100px;
height: 100px;
background-color: red;
margin-right: 10px;
font-size: 35px;
color: white;
float: left;
}
</style>
</head>
<body>
<div class="div1"></div>
<div class="div1" id="div2"></div>
<div class="div1"></div>
<div class="div1"></div>
</body>
</html>
<script type="text/javascript">
var odiv2 = document.getElementById("div2");
odiv2.onclick = function () {
alert(odiv2.nextSibling.nextSibling.nextSibling.nextSibling.innerText);
}
</script>

制作一个下拉菜单(仿qq)

<html >
<head>
<title></title>
<style type="text/css">
.div1 {
width: 100px;
height: 30px;
border: 2px solid black;
margin-top: 2px;
background-color: blue;
text-align: center;
line-height: 30px;
}
.div2 {
width: 100px;
height: 150px;
border: 2px solid black;
display: none; // 隐藏菜单不留位置
}
</style>
</head>
<body>
<div class="div1">好友</div>
<div class="div2"></div>
<div class="div1">家人</div>
<div class="div2"></div>
<div class="div1">同学</div>
<div class="div2"></div>
<div class="div1">二货</div>
<div class="div2"></div>
<div class="div1">陌生人</div>
<div class="div2"></div>
</body>
</html>
<script type="text/javascript">
var odiv1 = document.getElementsByClassName("div1");
for (var i = ; i < odiv1.length; i++) {
odiv1[i].onclick = function () {
//关上下拉菜单
if (this.nextSibling.nextSibling.style.display != "none") {
this.nextSibling.nextSibling.style.display = "none";
}
else {
for (var j = ; j < odiv1.length; j++) { //一次只能打开一个下拉菜单
odiv1[j].nextSibling.nextSibling.style.display = "none"
}
this.nextSibling.nextSibling.style.display = "block"
} //打开下拉菜单
}
}
</script>

效果图

3、父级 、子集元素

  var b = a.parentNodes;     // 找 a 的上一级父级元素

  var b = a.childNodes;        // 找出 a 的下一级子元素, 是一个数组

var b = a.childNodes;                  //第一个子元素

var b = a.lastChild;                   //最后一个

var b = a.childNodes[ n ]              //找第n个子元素

alert( nodes)[ i ] instanceof Text );
//判断是不是文本,是 true 不是 flase。

 4、创建添加元素

var  obj  =  document.createElement( "标签名" )    //  动态创建一个 Dom对象,创建一个元素。

var  obj  =  document.createElement( "div" )

a.appendChild( obj )      //向 a 中添加一个元素,添加在末尾

a.removeChild( obj )      //删除一个子元素。

JS DOM操作(五) Window.docunment对象——操作元素的更多相关文章

  1. JS DOM操作(二) Window.docunment对象——操作样式

    一 对元素的定位 在 js 中可以利用 id.class.name.标签名进行元素的定位 id.class  用在客户端 name  用在服务端 用 id 定位                  -- ...

  2. JS DOM操作(三) Window.docunment对象——操作属性

    属性:是对象的性质与对象之间关系的统称.HTML中标签可以拥有属性,属性为 HTML 元素提供附加信. 属性总是以名称/值对的形式出现,比如:name="value". 属性值始终 ...

  3. JS DOM操作(四) Window.docunment对象——操作内容

    操作内容:即对标签所夹内容的操作 一 非表单元素内容操作 定位 var a = document.ElementById( "id" ) 1.获取内容 var s = a.inne ...

  4. Excel VBA入门(五)Excel对象操作

    本章是本系列教程的重点.但我觉得应该不是难点.从第零章开始到学完本章,应该可以把VBA用于实战中了. Excel对象主要有4个: 工作薄 Workbook 工作表 Worksheet 单元格区域 Ra ...

  5. PHP中的MySQLi扩展学习(五)MySQLI_STMT对象操作

    就像 PDO 中的 PDO_Statment 对象一样,MySQLI_STMT 对象也是一个预处理语句所形成的对象,专门用来操作 MySQLi 所生成的预处理语句的.其实操作方式之类也都比较相似,不外 ...

  6. JavaScript的DOM操作。Window.document对象

    间隔执行一段代码:window.setlnteval("需要执行的代码",间隔毫秒数) 例 :      window.setlnteval("alert("你 ...

  7. JS中基本window.document对象操作以及常用事件!

    一.找到元素 1.document.getELementById("id"):根据id找,最多找一个. var a=document.getELementById("id ...

  8. DOM操作(Window.document对象)

    间隔与延迟: 间隔一段代码: window.setInterval("代码",间隔执行秒数) 延迟一段时间后执行一段代码: window.setTimeout("执行代码 ...

  9. DOM(五)事件对象

    浏览器中的事件都是以对象的形式存在的,同样ie浏览器与标准dom浏览器之间存在获取事件对象上也存在差别.在ie浏览器中事件对象是windows对象的一个属性event,访问通常采用如下方法. oP.o ...

随机推荐

  1. 定时任务 Wpf.Quartz.Demo.1

    Quartz 是个开源的作业调度框架. 安装:Install-Package Quartz 官网文档地址:https://www.quartz-scheduler.net/documentation/ ...

  2. WPF 打印界面(控件)到A4纸

    这次遇到一个需求,就是将整个界面打印在A4纸上. 需求清楚后,Bing一下关于打印,就找到一个类PrintDialog ,其中两个方法可能会用到: 特别是public void PrintVisual ...

  3. 轻量级MVVM框架 Stylet

    这两天试了下Stylet框架,这个框架虽然很小,但是功能齐全,简化了很多MVVM的代码,比如Command,对Dialog,MessageBox都有很好的支持. 开源地址 https://github ...

  4. Windows下安装配置爬虫工具Scrapy及爬虫环境

    爬虫工具Scrapy在Mac和Linux环境下都相对好装,但是在Windows上总会碰到各种莫名其妙的问题.本文记录下Scrapy在Window上的安装过程. 本文是基于Python2.7及Windo ...

  5. 关于IE9 table显示错位的问题

    首先,win10无法安装IE9,所以需要用IE11模拟IE9,这样:http://www.w10zj.com/Win10xy/Win10yh_638.html: 其次,table显示错位的可能原因:h ...

  6. java之Collection框架

    Collection的一些框架类的关系图: 1 Collection简介 Collection的定义 public interface Collection<E> extends Iter ...

  7. ST表的原理及其实现

    ST表类似树状数组,线段树这两种算法,是一种用于解决RMQ(Range Minimum/Maximum Query,即区间最值查询)问题的离线算法 与线段树相比,预处理复杂度同为O(nlogn),查询 ...

  8. Swift5 语言指南(二十六) 内存安全

    默认情况下,Swift可以防止代码中发生不安全行为.例如,Swift确保变量在使用之前进行初始化,在取消分配后不访问内存,并检查数组索引是否存在越界错误. Swift还确保对同一内存区域的多次访问不会 ...

  9. 【ElasticSearch】:elasticsearch.yml配置

    ElasticSearch5的elasticsearch.yml配置 注意 elasticsearch.yml中的配置,冒号和后面配置值之间有空格 cluster.name: my-applicati ...

  10. Network - Tips

    001 - 查询whois 可通过在线工具进行查询: https://www.whois365.com http://whois.aliyun.com http://whois.chinaz.com ...