操作属性:

对象.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. 【吐血整理】SVN命令行,Subversion的正确使用姿势,让版本控制更简单~

    一.写在前面 前面一直博主一直用svn的桌面版本,但看项目经理一直都用的命令行方式,不为性能,还能直接装逼呀!在这里先感谢赵哥,也把它分享给感兴趣的你们~ 二.直接上干货 1. svn checkou ...

  2. layer弹出层中H5播放器全屏出错解决 & 属性poster底图占满<video>的方法

    1. 在layer弹窗组件中 如果使用了flash播放器,全屏是正常的 但若使用了HTML5的播放器,全屏失效 举个栗子 <!DOCTYPE html> <html> < ...

  3. 今天遇到的面试题for(j=0,i=0;j<6,i<10;j++,i++) { k=i+j; } k 值最后是多少?

    for(j=0,i=0;j<6,i<10;j++,i++) { k=i+j; } k 值最后是多少? <script type="text/javascript" ...

  4. C++待解

    //[要求]按以下描述和要求建立一个含有对象成员的类TeleBook,用类Record定义的数组是TeleBook的数据成员. // 写出所有定义成员函数的代码.执行主函数对其测试. Record私有 ...

  5. 【图像浏览】FastStone Image Viewer——快速、小巧、功能强大

    FastStone Image Viewer 是一款免费(非商业用途)且小巧的看图软件. 其在在appinn.com的我最喜爱的<图片/照片浏览查看工具>调查结果中排第6名(总提名 140 ...

  6. POJ 2914 Minimum Cut Stoer Wagner 算法 无向图最小割

    POJ 2914 题意:给定一个无向图 小于500节点,和边的权值,求最小的代价将图拆为两个联通分量. Stoer Wagner算法: (1)用类似prim算法的方法求"最大生成树" ...

  7. Oracle 生成一张测试表并插入随机数据

    --生成随机表 --CREATE table scott.One_Million as ( SELECT ROWNUM AS T_ID, TRUNC(DBMS_RANDOM.VALUE(, )) 年龄 ...

  8. c#基础——for循环嵌套经典练习题(打★)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 作业题1 ...

  9. JavaScript设计模式之构造器、模块和原型

    JavaScript面向对象 JavaScript是一种无类语言,但可以使用函数来模拟,这就涉及到设计模式.模式是一种已经验证过的可复用的解决方案,可用于解决软件设计中遇到的常见的问题,通常将这些解决 ...

  10. Spring IOC以及三种注入方式

    IOC是spring的最基础部分,也是核心模块,Spring的其他组件模块和应用开发都是以它为基础的.IOC把spring的面向接口编程和松耦合的思想体现的淋漓尽致. IOC概念 IOC(Invers ...