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代码和文字都获取了,标签里面的所有内容。
如:body中有这么一个div:
<div id="me"><b>试试吧</b></div>
在script中用innerHTML获取div中的内容:
var a= document.getElementById("me");
alert(a.innerHTML);
结果如下图:
alert(a.innerText);只取里面的文字
alert(a.outerHTML);包括标签本身的内容(简单了解)
(2)设置内容:
a.innerHTML = "<font color=red >hello world </font>";
如果用设置内容代码结果如下,div中的内容被替换了:
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("属性名");移除一个属性。
例子1:做一个问题,如果输入的答案正确则弹出正确,错误弹出错误;
这里在text里面写了一个daan属性,里面存了答案的值,点击检查答案的时候cheak输入的内容和答案是否一样:
<form>中华民国成立于哪一年?<input type="text" daan="1912年" value="" id="t1" name="t1" /><input type="button" onclick="check()" id="t2" name="t2" value="检查答案" /></form>
body
function check()
{
var a=document.getElementById("t1");
var a1=a.value;
var a2=a.getAttribute("daan");
if(a1==a2)
{
alert("恭喜你答对了!");
}
else
{
alert("笨蛋!");
}
}
JS中
回答正确时的结果:
例子2: 同意按钮,倒计时10秒,同意按钮变为可提交的,这里用了操作属性:disabled,来改变按钮的状态,当disabled=”disabled”时按钮不可用。
<form><input type="submit" id="b1" name="b1" value="同意(10)" disabled="disabled" /></form>
body中
var n=10;
var a= document.getElementById("b1");
function bian()
{
n--;
if(n==0)
{
a.removeAttribute("disabled");
a.value="同意";
return;
}
else
{
a.value= "同意("+n+")";
window.setTimeout("bian()",1000);
}
}
window.setTimeout("bian()",1000);
JS中
四、操作样式
首先利用元素的ID找到该元素,存于一个变量中:
var a = document.getElementById("id");
然后可以对该元素的属性进行操作:
a.style="" ; 操作此ID样式的属性。
样式为CSS中的样式,所有的样式都可以用代码进行操作。
document.body.style.backgroundColor="颜色"; 整个窗口的背景色。
操作样式的class:a.className="样式表中的classname" 操作一批样式
例子1:展示图片的自动和手动切换;
<div id="tuijian" style=" background-image:url(imges/tj1.jpg);"> <div class="pages" id="p1" onclick="dodo(-1)"></div>
<div class="pages" id="p2" onclick="dodo(1)"></div></div>
body中
<script language="javascript">
var jpg =new Array();
jpg[0]="url(imges/tj1.jpg)";
jpg[1]="url(imges/tj2.jpg)";
jpg[2]="url(imges/tj3.jpg)";
var tjimg = document.getElementById("tuijian");
var xb=0;
var n=0;
function huan()
{
xb++;
if(xb == jpg.length)
{
xb=0;
}
tjimg.style.backgroundImage=jpg[xb];
if(n==0)
{
var id = window.setTimeout("huan()",3000);
}
}
function dodo(m)
{
n=1;
xb = xb+m;
if(xb < 0)
{
xb = jpg.length-1;
}
else if(xb >= jpg.length)
{
xb = 0;
}
tjimg.style.backgroundImage=jpg[xb];
}
window.setTimeout("huan()",3000);</script>
JS中
五、相关元素操作:
var a = document.getElementById("id");找到a;
var b = a.nextSibling,找a的下一个同辈元素,注意包含空格;
var b = a.previousSibling,找a的上一个同辈元素,注意包含空格;
var b = a.parentNode,找a的上一级父级元素;
var b = a.childNodes,找出来的是数组,找a的下一级子元素;
var b = a.firstChild,第一个子元素,lastChild最后一个,childNodes[n]找第几个;
alert(nodes[i] instanceof Text); 判断是不是文本,是返回true,不是返回flase,用if判断它的值是不是false,可以去除空格。
六、元素的创建、添加、删除:
var a = document.getElementById("id");找到a;
var obj = document.createElement("标签名");创建一个元素
obj.innerHTML = "hello world";添加的时候首先需要创建出一个元素。
a.appendChild(obj);向a中添加一个子元素。
a.removeChild(obj);删除一个子元素。
列表中a.selectedIndex:选中的是第几个;
//a.options[a.selectIndex]按下标取出第几个option对象
七、字符串的操作:
var s = new String(); 或var s ="aaaa";
var s = "hello world";
alert(s.toLowerCase());转小写 toUpperCase() 转大写
alert(s.substring(3,8));从第三个位置截取到第八个位置
alert(s.substr(3,8));从第三个位置开始截取,截取八个字符长度,不写后面的数字是截到最后.
s.split('');将字符换按照指定的字符拆开,放入数组,自动排序
s.length是属性
s.indexOf("world");world在字符串中第一次出现的位置,没有返回-1
s.lastIndexOf("o");o在字符串中最后一次出现的位置
八、日期时间的操作
var d = new Date();当前时间
d.setFullYear(2015,11,6);/*在想要设置的月份上减1设置*/
d.getFullYear:取年份;
d.getMonth():取月份,取出来的少1;
d.getDate():取天;
d.getDay():取星期几
d.getHours():取小时;
d.getMinutes():取分钟;d.getSeconds():取秒
d.setFullYear():设置年份,设置月份的时候注意-1。
九、数学函数的操作
Math.ceil();大于当前小数的最小整数
Math.floor();小鱼当前小数的最大整数
Math.sqrt();开平方
Math.round();四舍五入 (跟日常习惯一样不同于C#)
Math.random();随机数,0-1之间
十、小知识点
外面双引号,里面的双引号改为单引号;
在div里面行高设置时,无论设置多么高,所占用的行默认在中间位置(div上下区域内中间——【默认】垂直居中)。
文本框取出来的值是字符串,需要用parseint()转化为数字
s.match(reg); s代表一个字符串,reg代表一个字符串,两者进行匹配,如果两个字符串不匹配,返回一个null。
练习 带缩略图的图片轮播:
<style>
* {
margin: 0px;
padding: 0px;
}
#tuijian {
width: 476px;
height: 350px;
background-repeat: no-repeat;
background-position: center;
}
.pages {
top: 200px;
background-color: #000;
background-position: center;
background-repeat: no-repeat;
opacity: 0.4;
width: 100px;
height: 100px;
border-radius: 50px;
}
#p1 {
background-image: url(%E7%AE%AD%E5%A4%B4%E5%B7%A6.png);
float: left;
margin: 150px 0px 0px 10px;
}
#p2 {
background-image: url(%E7%AE%AD%E5%A4%B4%E5%8F%B3.png);
float: right;
margin: 150px 10px 0px 0px;
}
#gundong {
width: 800px;
height: 500px;
position: relative;
margin: 30px auto;
overflow: hidden;
}
#ta {
margin-left: 0px;
transition: 0.7s;
}
.ir {
width: 32px;
height: 32px;
position: absolute;
top: 230px;
z-index: 99;
}
#left {
left: 10px;
}
#right {
right: 10px;
}
.xiao {
width: 1080px;
height: 130px;
position: relative;
margin-top: 50px;
left: 450px;
}
.x1 {
width: 200px;
height: 125px;
position: relative;
float: left;
margin-left: 10px;
border: 3px solid #999;
}
css
<div id="gundong" onmouseover="pause()" onmouseout="conti()">
<table id="ta" cellpadding="0" cellspacing="0">
<tr height="500">
<td width="800"><img src="轮播/Images/1.jpg" /></td>
<td width="800"><img src="轮播/Images/2.jpg" /></td>
<td width="800"><img src="轮播/Images/3.jpg" /></td>
<td width="800"><img src="轮播/Images/4.jpg" /></td>
<td width="800"><img src="轮播/Images/5.jpg" /></td>
</tr>
</table>
<div class="ir" id="left" onclick="dong(-1)"><img src="轮播/Images/left.png" /></div>
<div class="ir" id="right" onclick="dong(1)"><img src="轮播/Images/right.png" /></div>
</div>
<div class="xiao">
<div id="d1" class="x1" onclick="dian(1)" style="border-color:#F00"><img width="200" src="轮播/Images/1.jpg" /></div>
<div id="d2" class="x1" onclick="dian(2)"><img width="200" src="轮播/Images/2.jpg" /></div>
<div id="d3" class="x1" onclick="dian(3)"><img width="200" src="轮播/Images/3.jpg" /></div>
<div id="d4" class="x1" onclick="dian(4)"><img width="200" src="轮播/Images/4.jpg" /></div>
<div id="d5" class="x1" onclick="dian(5)"><img width="200" src="轮播/Images/5.jpg" /></div>
</div>
body
<script>
function huan()
{
var tu=document.getElementById("ta");
var a=parseInt(tu.style.marginLeft);
if(a<=-3200)
{
tu.style.marginLeft="0px";}
else
{
tu.style.marginLeft=(a-800)+"px";}
qie(parseInt(tu.style.marginLeft));
shi=window.setTimeout("huan()",3000);
}
var shi=window.setTimeout("huan()",3000)
function pause()
{
window.clearTimeout(shi);}
function qie(a)
{
if(a==0)
{
document.getElementById("d1").style.borderColor="#F00";
document.getElementById("d2").style.borderColor="#999";
document.getElementById("d3").style.borderColor="#999";
document.getElementById("d4").style.borderColor="#999";
document.getElementById("d5").style.borderColor="#999";
}
else if(a==-800){
document.getElementById("d1").style.borderColor="#999";
document.getElementById("d2").style.borderColor="#F00";
document.getElementById("d3").style.borderColor="#999";
document.getElementById("d4").style.borderColor="#999";
document.getElementById("d5").style.borderColor="#999";
}
else if(a==-1600){
document.getElementById("d1").style.borderColor="#999";
document.getElementById("d2").style.borderColor="#999";
document.getElementById("d3").style.borderColor="#F00";
document.getElementById("d4").style.borderColor="#999";
document.getElementById("d5").style.borderColor="#999";
}
else if(a==-2400){
document.getElementById("d1").style.borderColor="#999";
document.getElementById("d2").style.borderColor="#999";
document.getElementById("d3").style.borderColor="#999";
document.getElementById("d4").style.borderColor="#F00";
document.getElementById("d5").style.borderColor="#999";
}
else if(a==-3200){
document.getElementById("d1").style.borderColor="#999";
document.getElementById("d2").style.borderColor="#999";
document.getElementById("d3").style.borderColor="#999";
document.getElementById("d4").style.borderColor="#999";
document.getElementById("d5").style.borderColor="#F00";
}
}
function dian(n)
{
var tu =document.getElementById("ta");
if(n==1){tu.style.marginLeft="0px";}
else if(n==2){tu.style.marginLeft="-800px"; }
else if(n==3){tu.style.marginLeft="-1600px";}
else if(n==4){tu.style.marginLeft="-2400px";}
else if(n==5){tu.style.marginLeft="-3200px";}
qie(parseInt(tu.style.marginLeft));
}
document.getElementById("ta").style.marginLeft="0px";
function conti()
{
shi=window.setTimeout("huan()",3000)}
function dong(m)
{
var tu=document.getElementById("ta");
var a=parseInt(tu.style.marginLeft);
if(m==-1)
{
if(a==0)
{tu.style.marginLeft=-3200+"px";
}
else
{
tu.style.marginLeft=(a+800)+"px";}
}
else
{
if(a==-3200)
{tu.style.marginLeft=0+"px";}
else
{tu.style.marginLeft=(a-800)+"px";}
}qie(parseInt(tu.style.marginLeft));
}
JS
JavaScript——DOM操作——Window.document对象的更多相关文章
- 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里面行高设置和整个外面高度一样,才能用竖直居中,居中是行居中 文本框取出来 ...
随机推荐
- 21335592 ROWS
CREATE TABLE w_big SELECT * FROM ( SEELCT * FROM w_tab UNION ALL SELECT * FROM w_tab_copy_modify ) ...
- [教程] 以本论坛为例,手把手教你使用按键精灵POST登陆网页
本帖最后由 isaacc 于 2012-2-26 11:08 编辑 整个操作,很无脑.只要你够勤快,你学不会,你来咬我.懒人和伸手党就直接复制代码去玩吧,但我不是叫你拿去干坏事. 准备工具:WPE和I ...
- Python学习笔记——1——基础知识
1.1.变量和算法 python语言很类似人类语言,变量不需要定义类型.比如: 整型 字符串类型 数组 Java int a=12 String s="test" String[] ...
- php 连接主从数据库
本代码是从uchome的代码修改的,是因为要解决uchome的效率而处理的.这个思维其实很久就有了,只是一直没有去做,相信也有人有同样的想法,如果有类似的,那真的希望提出相关的建议.封装的方式比较简单 ...
- Difference between _, __ and __xx__ in Python
When learning Python many people don't really understand why so much underlines in the beginning of ...
- -tableView: cellForRowAtIndexPath:方法不执行问题
今天在学习UItableView 的时候,定义了一个属性 @property (weak, nonatomic) NSMutableArray *dataList: 在ViewDidLoad方法方法中 ...
- Selenium2学习-024-WebUI自动化实战实例-022-网站不同分辨率下页面样式展示兼容性问题解决方案 -- 设置浏览器显示区域大小(无人值守,节约测试成本的福音,BOSS 最爱)
在 Web UI 自动化测试的过程中,通常会测试页面在不同分辨率下的显示效果,即在不同大小的显示器上全屏浏览器后的页面展示,此种测试方法需要购置不同大小的显示器,或者频繁的设置屏幕分辨率,不仅浪费了大 ...
- 如何将XML文件写入数据库
将xml文件转成string public string XMLDocumentToString(XmlDocument doc) { MemoryStream stream = new Memory ...
- C#中派生类调用基类构造函数用法分析
这里的默认构造函数是指在没有编写构造函数的情况下系统默认的无参构造函数 1.当基类中没有自己编写构造函数时,派生类默认的调用基类的默认构造函数例如: ? 1 2 3 4 5 6 7 8 9 10 11 ...
- .NET(c#)new关键字的三种用法
前几天去家公司面试,有一道这样的题:写出c#中new关键字的三种用法,思前想后挖空心思也只想出了两种用法,回来查了下msdn,还真是有第三种用法:用于在泛型声明中约束可能用作类型参数的参数的类型,这是 ...