基本的window.document操作及实例

找元素

1.根据id找

var d1 = document.getElementById("d1");

alert(d1);

2.根据class找

var d2 = document.getElementsByClassName("d");

alert(d2[1]);

3.根据标签名找

var d3 = document.getElementsByTagName("div");

alert(d3[0]);

4.根据name找

var d4 = document.getElementsByName("aa");

alert(d4[0]);

操作元素

操作内容

非表单元素

var d1 = document.getElementById("d1");

1.获取文本

alert(d1.innerText);

2.设置文本

d1.innerText = "hello";

3.获取html代码

alert(d1.innerHTML);

4.设置html代码

d1.innerHTML = "<b>加粗文字</b>";

表单元素

var b1 = document.getElementById("b1");

1.赋值

b1.value = "ceshi";

2.获取值

alert(b1.value);

操作属性

1.添加属性

var d1 = document.getElementById("d1");

d1.setAttribute("bs","1");

2.获取属性

alert(d1.getAttribute("cs"));

3.移除属性

d1.removeAttribute("cs");

操作样式

function showa()

{

1.获取样式,只能获取内联样式

var d3 = document.getElementById("d3");

alert(d3.style.color);

}

function set()

{

var d3 = document.getElementById("d3");

2.设置样式

d3.style.backgroundColor = "red";

}

注册按钮选中可使用:

<input type="checkbox" id="ck" onclick="xiugai()" />同意

<input type="button" value="注册" id="btn" disabled="disabled" />

JS程序:

function xiugai()

{

//找到复选框

var ck = document.getElementById("ck");

//找到按钮

var btn = document.getElementById("btn");

//判断复选框的选中状态

if(ck.checked)

{

//移除按钮的不可用属性

btn.removeAttribute("disabled");

}

else

{

//设置不可用属性

btn.setAttribute("disabled","disabled");

}

}

鼠标选中背景文字颜色改变:

Css样式:<style type="text/css">

#caidan{

width:500px; height:35px; border:1px solid #60F;

}

.xiang{

width:100px;

height:35px;

text-align:center;

line-height:35px;

vertical-align:middle;

float:left;

}

</style>

Body程序:

<div id="caidan">

<div class="xiang" onmouseover="huan(this)" >首页</div>

<div class="xiang" onmouseover="huan(this)" >产品中心</div>

<div class="xiang" onmouseover="huan(this)" >服务中心</div>

<div class="xiang" onmouseover="huan(this)" >联系我们</div>

</div>

JS程序:

function huan(a)

{

//将所有的项恢复原样式

var d = document.getElementsByClassName("xiang");

for(var i=0;i<d.length;i++)

{

d[i].style.backgroundColor="white";

d[i].style.color = "black";

}

//换该元素的样式

a.style.backgroundColor = "red";

a.style.color = "white";

}

倒计时结束按钮可点击:

<span id="daojishi">10</span>

<input disabled="disabled" type="button" value="注册" id="anniu" />

</div>

JS程序:

<script type="text/javascript">

window.setTimeout("daojishi()",1000);

//功能:倒计时减1

function daojishi()

{

//找到span

var s = document.getElementById("daojishi");

//判断

if(parseInt(s.innerHTML)<=0)

{

document.getElementById("anniu").removeAttribute("disabled");

}

else

{

//获取内容,减1之后再交给span

s.innerHTML = parseInt(s.innerHTML)-1;

//每隔一秒调一次该方法

window.setTimeout("daojishi()",1000);

}

}

</script>

基本的window.document操作及实例的更多相关文章

  1. JavaScript的DOM操作。Window.document对象

    间隔执行一段代码:window.setlnteval("需要执行的代码",间隔毫秒数) 例 :      window.setlnteval("alert("你 ...

  2. DOM操作(Window.document对象)

    间隔与延迟: 间隔一段代码: window.setInterval("代码",间隔执行秒数) 延迟一段时间后执行一段代码: window.setTimeout("执行代码 ...

  3. Selenium2学习-040-JavaScript弹出框(alert、confirm、prompt)操作演示实例

    弹出框是网页自动化测试常见得操作页面元素之一,常见的JavaScript弹出框有如下三种: 1.alert(message):方法用于显示带有一条指定消息和一个 OK 按钮的警告框.DemoAlert ...

  4. window.onload和window.document.readystate的探究

    在编写前端页面的时候,我们时常需要对页面加载的状态进行判断,以便进行相应的操作. 比如在移动端,时常需要在页面完全加载完成之前,先显示一个loading的图标,等待页面完成加载完成后,才显示出真正要展 ...

  5. js 浏览器窗口大小改变 高度 宽度获取 window/document.height()区别

    <script> //当浏览器的窗口大小被改变时触发的事件window.onresize window.onresize = function(){ console.log($(windo ...

  6. Window.document对象

    1.Window.document对象 一.找到元素: docunment.getElementById("id"):根据id找,最多找一个:     var a =docunme ...

  7. DOM、Window对象操作

    一.DOM的基本概念 DOM是文档对象模型,这种模型为树模型:文档是指标签文档:对象是指文档中每个元素:模型是指抽象化的东西. 一.基本语法: 数据类型(字符串,小数,整数,布尔,时间) var, v ...

  8. Window.document对象 轮播练习

    Window.document对象 一.找到元素:     docunment.getElementById("id"):根据id找,最多找一个:     var a =docun ...

  9. HTML Window.document对象

    1.Window.document对象 一.找到元素: docunment.getElementById("id"):根据id找,最多找一个:    var a =docunmen ...

随机推荐

  1. PHP DOS漏洞的新利用:CVE-2015-4024 Reviewed

    1.     背景介绍 今天我们想从2015.04.03的一个PHP远程dos漏洞(CVE-2015-4024)说起.技术细节见如下链接,https://bugs.php.net/bug.php?id ...

  2. MySQL查询和删除重复数据

    删除表中重复记录,只保留一条: delete from 表名 where 字段ID in (select * from (select max(字段ID) from 表名 group by 重复的字段 ...

  3. SVN Server for Migration

    SVN Server: http://mxsuse01/svn/repos/erp/Oracle_EMS Report SVN (Put to SVN Sort) 1. *.RDF 2. *CP.LD ...

  4. VB.net 2010 AndAlso,OrElse和And,Or的区别

    '************************************************************************* '**模 块 名:VB.net 2010 AndA ...

  5. 一图搞定【实战Java高并发程序设计】

    来了解下java并发的技术点吧.这里面包括了并发级别.算法.定律,还有开发包.在过去单核CPU时代,单任务在一个时间点只能执行单一程序,随着多核CPU的发展,并行程序开发就显得尤为重要.这本书主要介绍 ...

  6. 通过jquery js 实现幻灯片切换轮播效果

    观察各个电商网址轮播图的效果,总结了一下主要突破点与难点 1.->封装函数的步骤与具体实现 2->this关键字的指向 3->jquery js函数熟练运用 如animate 4-& ...

  7. Angular遇上CoffeeScript - NgComponent封装

    CoffeeScript是基于JavaScript的一门扩展小巧语言,它需要编译成JavaScript,然后再运行与浏览器或者Nodejs平台.JavaScript由于商业原因10天时间就匆忙诞生,所 ...

  8. ASP.NET MVC 控制器激活(一)

    ASP.NET MVC 控制器激活(一) 前言 在路由的篇章中讲解了路由的作用,讲着讲着就到了控制器部分了,从本篇开始来讲解MVC中的控制器,控制器是怎么来的?MVC框架对它做了什么?以及前面有的篇幅 ...

  9. 在VMWare中建立Hadoop虚拟集群的详细步骤(使用CentOS)

    最近在学习Hadoop,于是想使用VMWare建立一个虚拟的集群环境.网上有很多参考资料,但参照其步骤进行设置时却还是遇到了不少问题,所以在这里详细写一下我的配置过程,以及其中遇到的问题及相应的解决方 ...

  10. [Unity3D]自己动手重制坦克舰队ArmadaTank

    [Unity3D]自己动手重制坦克舰队ArmadaTank 我玩过一款坦克游戏ArmadaTank(坦克舰队),如下图所示 几个月前我尝试用Unity3D重制这款游戏,已经可以玩起来了.下面是在PC上 ...