JS获取对象代码示例
来源:互联网 发布:redis和mysql的区别 编辑:程序博客网 时间:2024/06/05 04:24
<!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>javascript获取对象封装</title>
<style type="text/css" media="all">
#test{
width:600px;
height:auto;
overflow:hidden;
padding:9px;
border:#FF0000 1px solid;
background-color:#F0F0F0;
}
ul{
border:#FFCC33 1px solid;
height:auto;
overflow:hidden;
width:560px;
background-color:#666666;
color:#FFFFFF;
margin:6px 0px;
padding:6px;
list-style-type:circle;
}
.test2{
background-color:#999999;
color:#333333;
}
input{
height:22px;
border:#FF9933 1px solid;
overflow:hidden;
cursor:pointer;
}
</style>
<script language="javascript" type="text/javascript">
/*-------------封装开始------------------*/
function GetId(id){return document.getElementById(id)} //通过ID获取对象
function GetTag(tag){return document.getElementsByTagName(tag)} //通过Tag获取对象
function GetChildTag(id,tag){return id.getElementsByTagName(tag)} //通过Tag获取ID的子对象
function GetClass(className){return getElementsByClassName(className)} //通过className获取对象
var $c=function(array){var nArray = [];for (var i=0;i<array.length;i++) nArray.push(array[i]);return nArray;};
Array.prototype.each=function(func){for(var i=0,l=this.length;i<l;i++) {func(this[i],i);};};
var getElementsByClassName=function(cn){
var hasClass=function(w,Name){
var hasClass = false;
w.className.split(' ').each(function(s){
if (s == Name) hasClass = true;
});
return hasClass;
};
var elems =document.getElementsByTagName("*")||document.all;
var elemList = [];
$c(elems).each(function(e){
if(hasClass(e,cn)){elemList.push(e);}
})
return $c(elemList);
};
/*-----------------封装结束---------------------*/
window.onload=function(){
GetTag("input")[0].onclick=function(){alert("id为test的div内容是:/n"+GetId("test").innerHTML)}
GetTag("input")[1].onclick=function(){alert("body里共有"+GetTag("li").length+"个li")}
GetTag("input")[2].onclick=function(){alert("DIV里共有"+GetChildTag(GetId("test"),"li").length+"个li")}
GetTag("input")[3].onclick=function(){alert("className为test2的内容是:/n"+GetClass("test2")[0].innerHTML+"/n"+GetClass("test2")[1].innerHTML)}
}
</script>
</head>
<body>
<div id="test" title="这是id=test的div">
<ul title="div内第一个ul">
<li>ccontent1</li>
<li>ccontent2</li>
<li>ccontent3</li>
<li>ccontent4</li>
</ul>
<ul class="test2" title="div内第二个ul,class=test2">
<li>bcontent1</li>
<li>bcontent2</li>
<li>bcontent3</li>
</ul>
</div>
<ul class="test2" title="div外的ul,class=test2">
<li>content1</li>
<li>content2</li>
</ul>
<input type="button" value="通过id获取" />
<input type="button" value="通过tag获取" />
<input type="button" value="通过id内tag获取" />
<input type="button" value="通过className获取" />
</body>
</html></font>
- JS获取对象代码示例
- js获取url中指定参数值的示例代码
- Js+Jq获取URL参数的集中方法示例代码
- Js获取数组最大和最小值示例代码
- jQuery(js)获取文字宽度(显示长度)示例代码
- 使用JS模板引擎、面向对象以及示例代码
- SqlDataAdapter对象代码示例
- JS分页代码示例
- js 300代码示例
- python面向对象代码示例
- JavaScript面向对象代码示例
- Python面向对象示例代码
- JS操作cookies示例代码
- JS正则表达式示例代码
- JS回车处理代码示例
- js获取对象
- js获取event对象
- 获取js事件对象
- jQuery Ajax调用过程
- Writing Windows Live Writer Plugins - A Recap
- 如果一个程序跑10000次只失败一次,你会怎么调试
- 灰色记忆
- 名言啊
- JS获取对象代码示例
- 师徒四人游
- Qt集成到VS2008
- openssl 0.9.7a-0.9.7d的一个多线程bug
- 多态性和虚函数知识点整理
- 仿GOOGLE个性首页可移动图层效果
- C#(.net)中的DllImport
- c++控制ie浏览器
- 简单的构造和析构函数 你能玩的转吗?