JavaScript(DOM操作)(Window.document对象)
一、找到元素:
docunment.getElementById("id"); 根据id找,最多找一个;
var a =docunment.getElementById("id"); 将找到的元素放在变量中;
docunment.getElementsByName("name"); 根据name找,找出来的是数组;
docunment.getElementsByTagName("name"); 根据标签名找,找出来的是数组;
docunment.getElementsByClassName("name") 根据classname找,找出来的是数组;
二、操作内容:
1. 非表单元素:
1)获取内容:
alert(a.innerHTML); 标签里的html代码和文字都获取了,标签里面的所有内容。
alert(a.innerText); 只取里面的文字
alert(a.outerHTML); 包括标签本身的内容(简单了解)
2)设置内容:
a.innerHTML = "<font color=red >hello world </font>";
a.innerText会将赋的东西原样呈现
清空内容:赋值个空字符串
2. 表单元素:
1)获取内容,有两种获取方式:
var t = document.f1.t1; form表单ID为f1里面的ID为t1的input;
var t = document.getElementById("id"); 直接用ID获取。
alert(t.value); 获取input中的value值;
alert(t.innerHTML); 获取<textarea> 这里的值 </textarea>;
2)设置内容: t.value="内容改变";
3. 一个小知识点:
<a href="http://www.baidu.com" onclick ="return false">转向百度</a> ;
加了return flase则不会跳转,默认是return true会跳转。按钮也一样,如果按钮中设置return flase 则不会进行提交,利用这个可以对提交跳转进行控制。
三、操作属性
首先利用元素的ID找到该元素,存于一个变量中:
var a = document.getElementById("id");
然后可以对该元素的属性进行操作:
a.setAttribute("属性名","属性值"); 设置一个属性,添加或更改都可以;
a.getAttribute("属性名"); 获取属性的值;
a.removeAttribute("属性名"); 移除一个属性。
四、操作样式
首先利用元素的ID找到该元素,存于一个变量中:
var a = document.getElementById("id");
然后可以对该元素的属性进行操作:
a.style="" ;操作此ID样式的属性。
样式为CSS中的样式,所有的样式都可以用代码进行操作。
document.body.style.backgroundColor="颜色"; 整个窗口的背景色。
操作样式的class:a.className="样式表中的classname" 操作一批样式
例:展示图片的自动和手动切换;
若要让图片轮播有过渡效果 则在放置大图的div标签里建立一个表格存放图片,用js控制表格的marginleft属性从而实现(手风琴
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
*
{
margin:0px;
padding:0px;}
body
{
min-width:900px;}
#datu
{
width:800px;
height:500px;
position:relative;
margin:30px auto;
overflow:hidden;}
.datu
{
width:1200px;
height:500px;
position:relative;
margin:30px auto;
overflow:hidden;}
#ta
{
margin-left:0px;
transition:.7s;}
.lr
{
position:absolute;
top:230px;
width:32px;
height:32px;
z-index:;}
#left
{
left:10px;
}
#right
{
right:10px;
}
.suolue
{
height:60px;
width:450px;
position:relative;
margin:auto;
line-height:60px;}
.suolue1
{
height:50px;
width:80px;
float:left;
border: 4px solid #;
background-size:contain;
background-position:center;}
.u1
{
list-style-type:none;}
.u1 li
{
float:left;
margin-left:2px;}
.u2
{
list-style-type:none;}
.u2 li
{
float:left;}
.fengqin
{
height:500px;
width:100px;
background-repeat:no-repeat;
background-position:center;
transition:.7s;}
</style>
</head> <body>
<div id="datu" onmouseover="pause()" onmouseout="conti()">
<table id="ta" cellpadding="" cellspacing="">
<tr height="">
<td width=""><img src="Images/1.jpg" /></td>
<td width=""><img src="Images/2.jpg" /></td>
<td width=""><img src="Images/3.jpg" /></td>
<td width=""><img src="Images/4.jpg" /></td>
<td width=""><img src="Images/5.jpg" /></td>
</tr>
</table>
<div class="lr" id="left" onclick="dong(-1)">
<img src="Images/left.png" />
</div>
<div class="lr" id="right" onclick="dong(1)">
<img src="Images/right.png" />
</div>
</div>
<div class="suolue">
<ul class="u1">
<li><div class="suolue1" style="background-image:url(Images/1.jpg); border-color:red" id="tu1" onclick="xiaotu(1)"></div></li>
<li><div class="suolue1" style="background-image:url(Images/2.jpg)" id="tu2" onclick="xiaotu(2)"></div></li>
<li><div class="suolue1" style="background-image:url(Images/3.jpg)" id="tu3" onclick="xiaotu(3)"></div></li>
<li><div class="suolue1" style="background-image:url(Images/4.jpg)" id="tu4" onclick="xiaotu(4)"></div></li>
<li><div class="suolue1" style="background-image:url(Images/5.jpg)" id="tu5" onclick="xiaotu(5)"></div></li>
</ul>
</div>
<div class="datu" onmouseover="stp()" onmouseout="goon()">
<ul class="u2">
<li><div class="fengqin" id="l1" onmouseover="fq(1)" style="width:800px; background-image:url(Images/1.jpg)"></div></li>
<li><div class="fengqin" id="l2" onmouseover="fq(2)" style="background-image:url(Images/2.jpg)"></div></li>
<li><div class="fengqin" id="l3" onmouseover="fq(3)" style="background-image:url(Images/3.jpg)"></div></li>
<li><div class="fengqin" id="l4" onmouseover="fq(4)" style="background-image:url(Images/4.jpg)"></div></li>
<li><div class="fengqin" id="l5" onmouseover="fq(5)" style="background-image:url(Images/5.jpg)"></div></li>
</ul>
</div>
</body>
</html>
<script>
document.getElementById("ta").style.marginLeft="0px";
var biao=;
function huan()
{
var tu =document.getElementById("ta");
var a=parseInt(tu.style.marginLeft);
if(a<=-)
{
tu.style.marginLeft="0px";
}
else
{
tu.style.marginLeft= (a-)+"px";
}
document.getElementById("tu1").style.borderColor="#666";
document.getElementById("tu2").style.borderColor="#666";
document.getElementById("tu3").style.borderColor="#666";
document.getElementById("tu4").style.borderColor="#666";
document.getElementById("tu5").style.borderColor="#666";
var b = parseInt(document.getElementById("ta").style.marginLeft);
if(b==)
{
document.getElementById("tu1").style.borderColor="red";
}
else if(b==-)
{
document.getElementById("tu2").style.borderColor="red";
}
else if(b==-)
{
document.getElementById("tu3").style.borderColor="red";
}
else if(b==-)
{
document.getElementById("tu4").style.borderColor="red";
}
else
{
document.getElementById("tu5").style.borderColor="red";
}
shi =window.setTimeout("huan()",);
}
var shi =window.setTimeout("huan()",); function pause()
{
window.clearTimeout(shi);
} function conti()
{
shi = window.setTimeout("huan()",);
} function dong(ad)
{
var tu =document.getElementById("ta");
var a=parseInt(tu.style.marginLeft);
if(ad==-)
{
if(a==)
{
tu.style.marginLeft=-+"px";
}
else
{
tu.style.marginLeft= (a+)+"px";
}
}
else
{
if(a==-)
{
tu.style.marginLeft=+"px";
}
else
{
tu.style.marginLeft= (a-)+"px";
}
}
document.getElementById("tu1").style.borderColor="#666";
document.getElementById("tu2").style.borderColor="#666";
document.getElementById("tu3").style.borderColor="#666";
document.getElementById("tu4").style.borderColor="#666";
document.getElementById("tu5").style.borderColor="#666";
var b = parseInt(document.getElementById("ta").style.marginLeft);
if(b==)
{
document.getElementById("tu1").style.borderColor="red";
}
else if(b==-)
{
document.getElementById("tu2").style.borderColor="red";
}
else if(b==-)
{
document.getElementById("tu3").style.borderColor="red";
}
else if(b==-)
{
document.getElementById("tu4").style.borderColor="red";
}
else
{
document.getElementById("tu5").style.borderColor="red";
}
}
function xiaotu(x)
{
document.getElementById("tu1").style.borderColor="#666";
document.getElementById("tu2").style.borderColor="#666";
document.getElementById("tu3").style.borderColor="#666";
document.getElementById("tu4").style.borderColor="#666";
document.getElementById("tu5").style.borderColor="#666";
if(x==)
{
document.getElementById("ta").style.marginLeft="0px";
document.getElementById("tu1").style.borderColor="red";
}
else if(x==)
{
document.getElementById("ta").style.marginLeft="-800px";
document.getElementById("tu2").style.borderColor="red";
}
else if(x==)
{
document.getElementById("ta").style.marginLeft="-1600px";
document.getElementById("tu3").style.borderColor="red";
}
else if(x==)
{
document.getElementById("ta").style.marginLeft="-2400px";
document.getElementById("tu4").style.borderColor="red";
}
else
{
document.getElementById("ta").style.marginLeft="-3200px";
document.getElementById("tu5").style.borderColor="red";
}
}
function fengqin()
{
var li1 =parseInt(document.getElementById("l1").style.width);
var li2 =parseInt(document.getElementById("l2").style.width);
var li3 =parseInt(document.getElementById("l3").style.width);
var li4 =parseInt(document.getElementById("l4").style.width);
var li5 =parseInt(document.getElementById("l5").style.width);
document.getElementById("l1").style.width="100px";
document.getElementById("l2").style.width="100px";
document.getElementById("l3").style.width="100px";
document.getElementById("l4").style.width="100px";
document.getElementById("l5").style.width="100px";
if(li1==)
{
document.getElementById("l2").style.width="800px";
}
else if(li2==)
{
document.getElementById("l3").style.width="800px";
}
else if(li3==)
{
document.getElementById("l4").style.width="800px";
}
else if(li4==)
{
document.getElementById("l5").style.width="800px";
}
else
{
document.getElementById("l1").style.width="800px";
}
change=window.setTimeout("fengqin()",);
}
function fq(c)
{
document.getElementById("l1").style.width="100px";
document.getElementById("l2").style.width="100px";
document.getElementById("l3").style.width="100px";
document.getElementById("l4").style.width="100px";
document.getElementById("l5").style.width="100px";
if(c==)
{
document.getElementById("l1").style.width="800px";
}
else if(c==)
{
document.getElementById("l2").style.width="800px";
}
else if(c==)
{
document.getElementById("l3").style.width="800px";
}
else if(c==)
{
document.getElementById("l4").style.width="800px";
}
else if(c==)
{
document.getElementById("l5").style.width="800px";
}
}
var change=window.setTimeout("fengqin()",);
function stp()
{
window.clearTimeout(change);
}
function goon()
{
change=window.setTimeout("fengqin()",);
} </script>


)
JavaScript(DOM操作)(Window.document对象)的更多相关文章
- JavaScript——DOM操作——Window.document对象
一.找到元素: docunment.getElementById("id"):根据id找,最多找一个: var a =docunment.getElementById(&qu ...
- JavaScript(四)——DOM操作——Window.document对象
一.找到元素: docunment.getElementById("id"):根据id找,最多找一个: var a =docunment.getElementById(&qu ...
- HTML中 DOM操作的Document 对象详解(收藏)
Document 对象Document 对象代表整个HTML 文档,可用来访问页面中的所有元素.Document 对象是 Window 对象的一个部分,可通过 window.document 属性来访 ...
- JavaScript的DOM操作。Window.document对象
间隔执行一段代码:window.setlnteval("需要执行的代码",间隔毫秒数) 例 : window.setlnteval("alert("你 ...
- javascript DOM 操作
在javascript中,经常会需要操作DOM操作,在此记录一下学习到DOM操作的知识. 一.JavaScript DOM 操作 1.1.DOM概念 DOM :Document Object Mode ...
- javascript DOM 操作 attribute 和 property 的区别
javascript DOM 操作 attribute 和 property 的区别 在做 URLRedirector 扩展时,注意到在使用 jquery 操作 checkbox 是否勾选时,用 at ...
- Window.document对象
1.Window.document对象 一.找到元素: docunment.getElementById("id"):根据id找,最多找一个: var a =docunme ...
- Window.document对象 轮播练习
Window.document对象 一.找到元素: docunment.getElementById("id"):根据id找,最多找一个: var a =docun ...
- HTML Window.document对象
1.Window.document对象 一.找到元素: docunment.getElementById("id"):根据id找,最多找一个: var a =docunmen ...
- JS中window.document对象
小知识点注:外面双引号,里面的双引号改为单引号: 在div里面行高设置和整个外面高度一样,才能用竖直居中,居中是行居中 文本框取出来 ...
随机推荐
- swift学习笔记之-方法部分
//方法部分 import UIKit //方法(Methods) /*方法是与某些特定类型相关联的函数,类.结构体.枚举都可以定义实例方法 实例方法(Instance Methods): 1.属于某 ...
- Javascript - Arraylike的7种实现
jQuery的崛起让ArrayLike(类数组)在javascript中大放异彩,它的出现为一组数据的行为(函数)扩展提供了基础. 类数组和数组相似,具有数组的某些行为,但是它相比数组可以更加自由的扩 ...
- linux命令学习使用记录
1.文件批量重命名:把所有.xml文件重命名.txt,第一个参数为文件名中字符串,第二个参数为替换后文件名,第三个为当前目录文件列表 rename .xml .txt *.xml 2.解压不显示过程: ...
- 阿里云上部署 centos+nodejs+mongodb
先执行 yum -y update nginx 安装 yum -y install nginx 设置开机启动 chkconfig nginx on nodejs 安装: yum install nod ...
- [Android]Activity启动过程
Android系统启动加载流程: 参考图 Linux内核加载完毕 启动init进程 init进程fork出zygote进程 zygote进程在ZygoteInit.main()中进行初始化的时候for ...
- umeng 渠道统计 android
1.配置AndroidManifest.xml,添加权限 <uses-permission android:name="android.permission.ACCESS_NETWOR ...
- iOS开发之动画中的时间
概述 在动画中,我们会指定动画的持续时间.例如 scaleAnimation.duration = self.config.appearDuration 那么这个时间是怎么定义的呢?是指的绝对时间吗? ...
- Android自定义控件2--优酷菜单界面初始化
本文开始将逐步去实现下面优酷菜单的效果: 本文地址:http://www.cnblogs.com/wuyudong/p/5912538.html,转载请注明源地址. 本文首先来实现优酷菜单界面初始化工 ...
- eclipse出现感叹号的解决办法
当eclipse导入项目出现红叉但无提示错误时,去看:1>菜单路径----Window/Show View/Console2>菜单路径----Window/Show View/Error ...
- 【读书笔记】iOS-使用Web Service-基于客户端服务器结构的网络通信(一)
Web Service技术是一种通过Web协议提供服务,保证不同平台的应用服务可以互操作,为客户端程序提供不同的服务. 目前3种主流的Web Service实现方案用:REST,SOAP和XML-RP ...