<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript">
function getDom01(){
var div = document.querySelector(".box"); //得到属性 class属性
console.log(div.className);
//得到的是值
console.log(div.getAttribute("class"));
//得到集合
var attrs = div.attributes;
for(var i=0;i<attrs.length;i++){
var attr = attrs[0];
console.log(attr.value);
} console.log("--------------------");
console.log(div.nodeName);
console.log(div.nodeType);
console.log(div.nodeValue); //console.log(div)
} function setAttr(){
var div = document.querySelector(".box");
div['test'] = "ttttt";
//设置属性的值
div.setAttribute("testtest","tttttt");
//div.setAttribute("name","divdiv");
div.name = "divdiv";
div.removeAttribute("name");
} function setTextText(){
var div = document.querySelector(".box");
//console.log(div.textContent)
console.log(div.innerHTML);
//改变文本里面的额内容
div.innerHTML = "<h1>this is a innerHTML test</h1>";
} /**
*
* 元素节点 属性节点 文本节点
*
* NodeName 元素名 属性名 #text
*
* NodeType 1 2 3
*
* NodeValue null 属性值 文本内容
*
*/ //得到所有的子元素
function getChilds(_parent){
var childs = [];
if(_parent){
var child = _parent.childNodes;
for(var i=0;i<child.length;i++){
var c = child[i];
if(c.nodeType === 1){
childs.push(c);
}
}
}
return childs;
} function testGetChilds(){
var childs = getChilds(document.querySelector(".box"));
console.log(childs)
}
/**
* 通用的得到上一个下一个元素
*/
function getSibling(_dom,sibling){
if(_dom){
sibling = sibling || "nextSibling";
_dom = _dom[sibling];
while(_dom && _dom.nodeType != 1) {
_dom = _dom[sibling];
}
return _dom;
}
return null;
} //得到同级的下一个元素
function getNextSibling(_dom){
if(_dom){
_dom = _dom.nextSibling;
while(_dom.nodeType != 1){
_dom = _dom.nextSibling;
}
return _dom;
}
return null;
} function testGetSibling(){
console.log(getSibling(document.querySelector(".box"),"previousSibling"))
}
//获取上一个节点
function getPreviousSibling(_dom){
return getSibling(_dom,"previousSibling");
} function testStyle(){
var div = document.querySelector("#box2");
div.style.border = "1px solid red";
}
function testStyle1(){
var div = document.querySelector("#box2");
//改变样式的注意事项
//当不是一个规则单词的时候
div.style.border = "none";
//用中括号赋值
div.style["margin-left"] = "10px";
//满足驼峰命名
div.style.marginRight = "20px";
} </script>
</head>
<body>
<input type="button" onclick="getDom01()" value="getDom01"/>
<input type="button" onclick="setAttr()" value="setAttr"/>
<input type="button" onclick="setTextText()" value="setTextText"/>
<input type="button" onclick="testGetChilds()" value="testGetChilds"/>
<input type="button" onclick="testGetSibling()" value="testGetSibling"/>
<input type="button" onclick="testStyle()" value="testStyle"/>
<input type="button" onclick="testStyle1()" value="testStyle1"/>
<hr/>
<div class="box" name="div">
<div id="box1" style="">
<span>this is a span in div 1</span>
<span>this is a span in div 2</span>
</div>
<div id="box2">
<span>this is a span in div 1</span>
<span>this is a span in div 2</span>
</div>
<div id="box3">
<span>this is a span in div 1</span>
<span>this is a span in div 2</span>
</div>
<div id="box4">
<span>this is a span in div 1</span>
<span>this is a span in div 2</span>
</div>
</div>
</body>
</html>

js中获取父节点,兄弟节点及处理属性节点的更多相关文章

  1. js中获取 table节点各tr及td的内容方法

    js中获取 table节点各tr及td的内容方法 分类: java基础2013-10-12 17:54 1055人阅读 评论(0) 收藏 举报 <table id="tb1" ...

  2. Js/Jquery获取iframe中的元素 在Iframe中获取父窗体的元素方法

    在web开发中,经常会用到iframe,难免会碰到需要在父窗口中使用iframe中的元素.或者在iframe框架中使用父窗口的元素 js 在父窗口中获取iframe中的元素  1. 格式:window ...

  3. js获取iframe中的元素以及在iframe中获取父级的元素(包括iframe中不存在name和id的情况)

      第一种情况:iframe中不存在name和id的方法:(通过contentWindow获取) var iframe = document.getElementsByTagName('iframe' ...

  4. js中获取css样式属性值

    关于js中style,currentStyle和getComputedStyle几个注意的地方 (1)用js的style只能获取元素的内联样式,内部样式和外部样式使用style是获取不到的.针对css ...

  5. JS中获取元素属性的逆天大法

    给大家聊下js中获取元素属性的逆天大法,胆小慎入,切记切记!!! innerHTML.outerHTML.innerText .outerText.value.text().html(),val() ...

  6. js中获取URL中指定的查询字符串

    js中获取URL中指定的搜索字符串,主要利用location对象实现,废话少说,上代码. function getSearchString(key) { // 获取URL中?之后的字符 var str ...

  7. js中获取css属性

    直接获取 window.onload = function() { var but = document.getElementById('button'); var div = document.ge ...

  8. 【2017-06-27】Js中获取地址栏参数、Js中字符串截取

    一.Js中获取地址栏参数 //从地址栏获取想要的参数 function GetQueryString(name) { var reg = new RegExp("(^|&)" ...

  9. 小程序 js中获取时间new date()的用法(网络复制过来自用)

    js中获取时间new date()的用法   获取时间: 1 var myDate = new Date();//获取系统当前时间 获取特定格式的时间: 1 myDate.getYear(); //获 ...

随机推荐

  1. 在CMD中使用for命令对单行字符串做分割的方法

    我们都知道CMD中的for命令是执行循环命令的,数据来源可以是一个文件,一个命令的结果或一个字符串,只有这3种来源 如果是一个文件则对这个文件的所有字符串进行循环处理 如果是一个命令结果,那么对这个命 ...

  2. L173

    Technical problems temporarily blocked some US and European users having access to their accounts an ...

  3. Locust 关联

    关联的概念 用户登入后,服务器通常会给用户返回一个sessionID, 用户每次登陆,服务器返回的都会不同. 那么在自动化测试中,让系统自动登入账号就会被中断.那么我们可以通过取得服务器每次返回的se ...

  4. 编写 Target 检测 MSBuild / dotnet build 此次编译是否是差量编译

    MSBuild 或 Roslyn 编译项目时均支持差量编译,毕竟为了性能.我在 每次都要重新编译?太慢!让跨平台的 MSBuild/dotnet build 的 Target 支持差量编译 一文中介绍 ...

  5. python 中datetime 和 string 转换

    dt = datetime.datetime.strptime(string_date, fmt) fmt 的格式说明如下: https://docs.python.org/2/library/dat ...

  6. 便捷的Jenkins jswidgets

    很多时候我们在构建完成之后需要查看构建的状态,类似github 中的build Status 插件安装 搜索插件 使用 目前好像只支持自由项目的构建 代码集成 <!DOCTYPE html> ...

  7. Loararunner录制脚本

    LoadRunner录制 1.启动LoadRunner,用管理员方式打开,选择 “Create/Edit Scripts” 2.从这两个方式任意方式打开脚本页面 3.选择协议,这里我们举例子,用“We ...

  8. windows7安装django并创建第一个应用

    1.安装django 1.1.下载Django包 https://www.djangoproject.com/download/https://www.djangoproject.com/m/rele ...

  9. debian下为apache启用rewrite模块

    如果我们是自己编译的apache,那么启用或禁用某个模块应该说是比较容易的事,只要修改apache的配置文件就可以了.但是我们没有理由不用已经做好的二进制文件进行安装,使用apt-get要方便多了. ...

  10. 用arm-linux-gnueabihf移植MP3播放器libmad-0.15.1b的时候出现错误提示

    diff --git a/package/libmad/libmad-0.15.1b-thumb2-fixed-arm.patch b/package/libmad/libmad-0.15.1b-th ...