六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 102|回复: 0

获得HTML元素的3种方法

[复制链接]

升级  27.33%

25

主题

25

主题

25

主题

秀才

Rank: 2

积分
91
 楼主| 发表于 2013-2-7 21:02:48 | 显示全部楼层 |阅读模式
一.getElementById方法
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>无标题文档</title><script type="text/javascript" >function showText(){var oText = document.getElementById("my_text");//得到文本框元素oText.value = "haha";//向文本框中赋值}</script></head><body><input type="text" id="my_text" /><input type="button"  value="向文本框中赋值" /></body> 
二.getElementsByName方法
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>无标题文档</title><script type="text/javascript" >function showText(){var oText = document.getElementsByName("txt");//得到所有name为txt的元素集合for(var i = 0; i < oText.length; i++){oText.value = i;}}</script></head><body><input type="text" name="txt" /><input type="text" name="txt" /><input type="text" name="txt" /><input type="text" name="txt" /><input type="text" name="txt" /><input type="text" name="txt" /><input type="button"  value="向文本框中赋值" /></body> 
 
三.getElementsByTagName方法
<title>无标题文档</title><script type="text/javascript" >function showText(){var oText = document.getElementsByTagName("input");//得到所有input对象for(var i = 0; i < oText.length; i++){alert(oText.type);//显示该节点的类型}}</script></head><body><input type="text" name="txt" /><input type="checkbox" name="cb" /><input type="password" name="pwd"  /><input type="button"  value="显示" /></body> 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表