码迷,mamicode.com
首页 > Windows程序 > 详细

window.document 对象

时间:2015-09-17 00:55:39      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

<!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>
<script type="text/javascript">

function getById(id)
{
//根据id查找

var aa = document.getElementById(id);

aa.style.backgroundColor="#FFF";

alert(aa);
/*

alert("innerHTML = " + aa.innerHTML);
alert("outerHTML" + aa.outerHTML);
alert("innerText" + aa.innerText);

aa.innerText = "";*/



}

function getByName(name)
{
//根据id查找
//var aa = document.getElementById(id);


var aa = document.getElementsByName(name);

alert(aa);

if (aa.length > 0)
{
	alert(aa[0].innerText);
}

/*alert("innerHTML = " + aa.innerHTML);
alert("outerHTML" + aa.outerHTML);
alert("innerText" + aa.innerText);

aa.innerText = "";*/
}

function getByTagName(name)
{
var aa = document.getElementsByTagName(name);

alert(aa);

if (aa.length > 0)
{
	for (var i = 0; i < aa.length; i++)
	alert(aa[i].innerText);
}

/*alert("innerHTML = " + aa.innerHTML);
alert("outerHTML" + aa.outerHTML);
alert("innerText" + aa.innerText);

aa.innerText = "";*/
}

function getByClassName(name)
{
var aa = document.getElementsByClassName(name);

alert(aa);

if (aa.length > 0)
{
	for (var i = 0; i < aa.length; i++)
	alert(aa[i].innerText);
}

/*alert("innerHTML = " + aa.innerHTML);
alert("outerHTML" + aa.outerHTML);
alert("innerText" + aa.innerText);

aa.innerText = "";*/
}

function getByForm()
{
	try
	{
	var aa = document.myForm.f6;
	
	alert(aa.value);
	
	aa.value = "新的内容";
	
	aa.setAttribute("newvalue", "新属性值");
	
	
	var bb = aa.getAttribute("daan")
	
	alert(bb);
	
	aa.removeAttribute("newvalue");
	
	bb = aa.getAttribute("newvalue");
	
	
	
	alert(bb);
	}
	catch(ex)
	{
		alert(ex);
	}
}


</script>
</head>

<body>

<div style="background-color:#F00; height:100px;" id="a" name="b" ><b>这是另一个元素</b></div>
<div class="c" id="ab"><b>这是一个元素</b></div>

<form  name="myForm" action="#" method="get" >




<br />

<input id="f1" type="button" onclick=‘getById("a");‘ value="根据ID查找元素" />

<br />

<input id="f2"  type="button" onclick=‘getByName("b");‘ value="根据Name查找元素" />

<br />

<input id="f3"  type="button" onclick=‘getByTagName("div");‘ value="根据TagName查找元素" />

<br />

<input id="f4"  type="button" onclick=‘getByClassName("c");‘ value="根据ClassName查找元素" />

<br />
<input id="f6" daan="1921年" type="text" width="30" value="内容" />

</form>

<br />

<input id="f5"  type="button" onclick=‘getByForm();‘ value="查找表单元素" />




</body>
</html>

 

window.document 对象

标签:

原文地址:http://www.cnblogs.com/hanke123/p/4814963.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!