操作属性:

对象.setAttribute('属性名','值'); - 添加属性
对象.getAttribute('属性名'); - 获取属性值,如无此属性,那么返回null

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style type="text/css">
.div1 {
width: 100px;
height: 50px;
float: left;
margin-right: 10px;
}
</style>
</head>
<body>
<div class="div1" dd=""></div>
<div class="div1" dd=""></div>
<div class="div1" dd=""></div>
<div class="div1" dd=""></div>
<div class="div1"></div>
<div class="div1"></div> </body>
</html>
<script type="text/javascript">
var aa = document.getElementsByClassName("div1");
for (var i = ; i < aa.length; i++) {
if (aa[i].getAttribute("dd") == "")
aa[i].style.backgroundColor = "green";
else if (aa[i].getAttribute("dd") == "")
aa[i].style.backgroundColor = "yellow";
else
aa[i].style.backgroundColor = "red"; }
</script>

对象.removeAttribute('属性名'); - 移除属性

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<input type="button" value="按钮111" id="dd1" />
<input type="button" value="按钮222" id="dd2" /> </body>
</html>
<script type="text/javascript">
var aaa = document.getElementById("dd1");
var bbb = document.getElementById("dd2");
//按钮111的点击事件
aaa.onclick = function () {
//按钮111添加一个属性不可用
this.setAttribute("disabled", "disabled");
//获取aaa中属性value的值
var ccc = this.getAttribute("value");
alert(ccc);
}
//bbb的点击事件
bbb.onclick = function () {
//移除aaa的disabled属性
aaa.removeAttribute("disabled");
} </script>

2.验证5+5=?

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
+=
<input type="text" id="dd1" data="" />
<input type="button" id="dd2" value="验证"/> </body>
</html>
<script type="text/javascript">
var aaa = document.getElementById("dd1");
var bbb = document.getElementById("dd2");
//按钮111的点击事件 //bbb的点击事件
bbb.onclick = function () {
var txt = aaa.getAttribute("data"); if (txt == aaa.value)
alert("正确");
else
alert("笨蛋");
} </script>

3.彩虹导航栏,移入变为灰色,移除变为原来的颜色。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style type="text/css">
.div1 {
width: 100px;
height: 50px;
float: left;
margin-right: 10px;
}
</style>
</head>
<body>
<div class="div1" style="background-color: red" dd="red"></div>
<div class="div1" style="background-color: blue" dd="blue"></div>
<div class="div1" style="background-color: green" dd="green"></div>
<div class="div1" style="background-color: pink" dd="pink"></div>
<div class="div1" style="background-color: purple" dd="purple"></div>
<div class="div1" style="background-color: yellow" dd="yellow"></div> </body>
</html>
<script type="text/javascript"> var aaa = document.getElementsByClassName("div1");
//中间变量存颜色
var color;
for (var i = ; i < aaa.length; i++) {
//鼠标移入事件
aaa[i].onmouseover = function () {
color = this.style.backgroundColor; this.style.backgroundColor = "gray";
}
//鼠标移除事件
aaa[i].onmouseout = function () {
this.style.backgroundColor = color;
} //鼠标点击事件
} </script>

定时器:
window.setTimeout(function(){},间隔时间毫秒);
- 定时炸弹,延迟执行,只执行一次

window.setInterval(function(){},间隔的时间毫秒);
- 无限循环,每一次循环有间隔时间,一般不要小于20毫秒
- 它是有返回值的,可以用一个变量来接收这个定时器对象

window.clearInterval(要关闭的定时器对象);
一旦执行这句代码,会立刻停止此定时器对象的执行

对象.offsetWidth

JS——操作属性的更多相关文章

  1. js 操作属性

    操作属性: 对象.setAttribute('属性名','值'); - 添加属性 对象.getAttribute('属性名'); - 获取属性值,如无此属性,那么返回null 对象.removeAtt ...

  2. js 操作属性,操作内容,

    disable=“disable” 让按钮变得不可选 先建一个按钮,让class = ’btn‘ 然后, 添加,修改属性 document.getElementsByClassName('btn')[ ...

  3. document--文档中的操作,操作属性、操作样式、操作元素

    ---恢复内容开始--- document操作:    1.找元素   getE..    2.操作内容   非表单:innerHtml   表单:value    3.操作属性            ...

  4. jquery 与javascript关系 ①取元素 ②操作内容 ③操作属性 ④操作 样式 ⑤ 事件 点击变色

    jQuery的min版本和原版功能是一样的,min版主要应用于已经开发成的网页中,而非min版 的文件比较大,里面有整洁的代码书写规范和注释,主要应用于脚本开发过程当中. JQuery是继protot ...

  5. 2016/4/1 jquery 与javascript关系 ①取元素 ②操作内容 ③操作属性 ④操作 样式 ⑤ 事件 点击变色

    jQuery的min版本和原版功能是一样的,min版主要应用于已经开发成的网页中,而非min版 的文件比较大,里面有整洁的代码书写规范和注释,主要应用于脚本开发过程当中. JQuery是继protot ...

  6. JS操作css的float属性的特殊写法

    使用js操作css属性的写法是有一定的规律的: 1.对于没有中划线的css属性一般直接使用style.属性名即可. 如:obj.style.margin,obj.style.width,obj.sty ...

  7. js介绍,js三种引入方式,js选择器,js四种调试方式,js操作页面文档DOM(修改文本,修改css样式,修改属性)

    js介绍 js运行编写在浏览器上的脚本语言(外挂,具有逻辑性) 脚本语言:运行在浏览器上的独立的代码块(具有逻辑性) 操作BOM 浏览器对象盒子 操作DOM 文本对象 js三种引入方式 (1)行间式: ...

  8. JS之document对象(找元素、操作内容、操作属性、操作样式及4道例题)

    document对象 一.找元素 1.根据id找 示例: <input id = "a" type="button" value="找元素&qu ...

  9. vue.js操作元素属性

    vue动态操作div的class 看代码: <!doctype html> <html lang="en"> <head> <meta c ...

随机推荐

  1. Omi教程-使用group-data通讯

    写在前面 Omi框架组建间的通讯非常遍历灵活,上篇文章介绍了几种通讯方式,其中childrenData的方式可以批量传递数据给组件,但是有很多场景下data的来源不一定非要都从childrenData ...

  2. leetcode-1006 Construct Binary Tree from Inorder and Postorder Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  3. css中书写小三角

    我们在开发过程中,有很多的方向标签不是图片,而是用css方法书写上去的. 首先我们要了解原理,border的边框的脚步是45度角. 向左方向: width:0px: height:0px: borde ...

  4. OpenGL理解

    说起编程作图,大概还有很多人想起TC的#include <graphics.h>吧? 但是各位是否想过,那些画面绚丽的PC游戏是如何编写出来的?就靠TC那可怜的640*480分辨率.16色 ...

  5. [bzoj1067][SCOI2007]降雨量——线段树+乱搞

    题目大意 传送门 题解 我国古代有一句俗话. 骗分出奇迹,乱搞最神奇! 这句话在这道题上得到了鲜明的体现. 我的方法就是魔改版线段树,乱搞搞一下,首先借鉴了黄学长的建树方法,直接用一个节点维护年份的区 ...

  6. P1137 旅行计划-----洛谷

    用了STL,其他的没什么,赤裸裸的拓扑排序 下面给出代码 #include<cstdio> #include<iostream> #include<vector> ...

  7. Nginx http 500错误分析及解决方法

      出现场景:   在用nginx做负载均衡服务器对系统做并发测试,并发量比较大时Nginx会报出Http 500错误     报错原因:   访问量大的时候,由于系统资源限制,而不能打开过多的文件 ...

  8. Python求解数组重新组合求最小值(优酷)

    题目描述:题目:含有n个元素的整型数组,将这个n个元素重新组合,求出最小的数,如(321,3,32,) 最小数为321323 题目分析: 将数组中所有元素填充到个数相等,填充的数字为最后一位的数字,如 ...

  9. Kubernetes运维生态-Heapster分析

    Heapster在Kubernetes的运维生态中如下:集群的容器的监控数据收敛汇聚层 heapster1.0版本后内部分为event和metric两个进程,可制作为两个docker镜像部署为两个独立 ...

  10. 第35篇 IIS执行原理

    服务器的监听(IIS6.0+版本) 当请求到达服务器时,请求最终会到达TCPIP.SYS驱动程序,TCPIP.SYS将请求转发给HTTP.SYS网络驱动程序的请求队列中(可以理解为专门处理http请求 ...