JS——操作属性
操作属性:
对象.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——操作属性的更多相关文章
- js 操作属性
操作属性: 对象.setAttribute('属性名','值'); - 添加属性 对象.getAttribute('属性名'); - 获取属性值,如无此属性,那么返回null 对象.removeAtt ...
- js 操作属性,操作内容,
disable=“disable” 让按钮变得不可选 先建一个按钮,让class = ’btn‘ 然后, 添加,修改属性 document.getElementsByClassName('btn')[ ...
- document--文档中的操作,操作属性、操作样式、操作元素
---恢复内容开始--- document操作: 1.找元素 getE.. 2.操作内容 非表单:innerHtml 表单:value 3.操作属性 ...
- jquery 与javascript关系 ①取元素 ②操作内容 ③操作属性 ④操作 样式 ⑤ 事件 点击变色
jQuery的min版本和原版功能是一样的,min版主要应用于已经开发成的网页中,而非min版 的文件比较大,里面有整洁的代码书写规范和注释,主要应用于脚本开发过程当中. JQuery是继protot ...
- 2016/4/1 jquery 与javascript关系 ①取元素 ②操作内容 ③操作属性 ④操作 样式 ⑤ 事件 点击变色
jQuery的min版本和原版功能是一样的,min版主要应用于已经开发成的网页中,而非min版 的文件比较大,里面有整洁的代码书写规范和注释,主要应用于脚本开发过程当中. JQuery是继protot ...
- JS操作css的float属性的特殊写法
使用js操作css属性的写法是有一定的规律的: 1.对于没有中划线的css属性一般直接使用style.属性名即可. 如:obj.style.margin,obj.style.width,obj.sty ...
- js介绍,js三种引入方式,js选择器,js四种调试方式,js操作页面文档DOM(修改文本,修改css样式,修改属性)
js介绍 js运行编写在浏览器上的脚本语言(外挂,具有逻辑性) 脚本语言:运行在浏览器上的独立的代码块(具有逻辑性) 操作BOM 浏览器对象盒子 操作DOM 文本对象 js三种引入方式 (1)行间式: ...
- JS之document对象(找元素、操作内容、操作属性、操作样式及4道例题)
document对象 一.找元素 1.根据id找 示例: <input id = "a" type="button" value="找元素&qu ...
- vue.js操作元素属性
vue动态操作div的class 看代码: <!doctype html> <html lang="en"> <head> <meta c ...
随机推荐
- matab plot指令和低通滤波器的响应图
一.plot额外的四个属性模板使用 代码 % 提示 disp ('该功能练习plot额外四个属性功能'); %初始化快捷式数组 figure(); x=:pi/:*pi; y=exp(*sin(x)) ...
- HTML中的a标签实现点击下载
通常在咱们写项目的时候会遇到上传下载什么的,在上传完文件后会把文件的路径保存到数据库里以便下载,如果想不通过后台直接下载的话,可以把文件路径给a标签的属性href: <a href=" ...
- Linux输入子系统(一) _驱动编码
输入设备都有共性:中断驱动+字符IO,基于分层的思想,Linux内核将这些设备的公有的部分提取出来,基于cdev提供接口,设计了输入子系统,所有使用输入子系统构建的设备都使用主设备号13,同时输入子系 ...
- EM and GMM(Theory)
Part 1: Theory 目录: What's GMM? How to solve GMM? What's EM? Explanation of the result What's GMM? GM ...
- AMD的cpu如何安装Mac OS
AMD的cpu如何安装Mac OS 经过一个月的折腾,终于成功的安装了自己的Mac系统,并且能够成功的启动.在这里分享一下自己安装的经验. 为了安装方便,有不影响原来的系统的使用 ...
- Win32/MFC/COM学习推荐书籍
以前有不少朋友问关于学习各种技术的推荐书籍的问题,这里把我觉得比较好的一些书籍列一下,希望能起到抛砖引玉的作用就好了:) Win32开发 Programming Windows by Charles ...
- jQuery源码学习:Sizzle
本文所有讨论均基于jQuery版本3.1.1,官网http://jquery.com/. 一 简介 Sizzle是用javascript实现的CSS selector engine,官网见https: ...
- Jemter性能测试
Jmeter 介绍 Jmeter 是一款使用Java开发的,开源免费的,测试工具, 主要用来做功能测试和性能测试(压力测试/负载测试). 而且用Jmeter 来测试 Restful API, 非常好 ...
- linux下keepalived 安装配置
keepalived是一个类似于layer3, 4 & 7交换机制的软件,也就是我们平时说的第3层.第4层和第7层交换.Keepalived的作用是检测web服务器的状态,如果有一台web服务 ...
- WPF DataGrid Drag
自己实现的功能.代码比较简单的DataGrid的Drag处理,着重处理DataGrid里的拖动排序. using System; using System.Collections.Generic; u ...