六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 34|回复: 0

DOM模型获得元素的值

[复制链接]

升级  30.33%

87

主题

87

主题

87

主题

举人

Rank: 3Rank: 3

积分
291
 楼主| 发表于 2013-1-23 01:46:58 | 显示全部楼层 |阅读模式
第一个简单的例子(getElementById()):<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>根据ID来访问HTML元素</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><script type="text/javascript">function accessById(){var a = document.getElementById("a").innerHTML;var b = document.getElementById("b").value;     //这里要注意Value和value的区别alert(a + "\n" + b);}</script>  </head>    <body>    <div id="a">我是一个学生</div>    <textarea rows="3" cols="12" id="b">我要好好学习JAVA</textarea><br>    <input type="button" value="访问这两个元素" >  </body></html><h2> 

第二个根据根节点访问HTML属性

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>根据节点关系访问HTML元素</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><style type="text/css">/* 定义改变背景色的CSS,表示被选中的项  */.selected{background-color: #6666ff}</style>  </head>    <body><ol id="book"><li id="java">JAVA</li><li id="ssh">SSH</li><li id="ajax" class="selected">Ajax</li><li id="xml">XML</li><li id="ejb">EJB</li><li id="jsp">JSP</li></ol><input type="button" value="父节点"  /><input type="button" value="第一个"  /><input type="button" value="上一个"  /><input type="button" value="下一个"  /><input type="button" value="最后一个"  /><script type="text/javascript">var currentTarget = document.getElementById("ajax");function change(target){alert(target.innerHTML);}</script>  </body></html> 

访问列表项:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>访问列表项</title>    <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page">  </head>    <body>    <select id="select1" name="select1" size="6">    <option value="java">JAVA</option>    <option value="ssh">SSH</option>    <option value="ajax">Ajax</option>    <option value="xml">XML</option>    <option value="ejb">EJB</option>    <option value="jsp">JSP</option>    </select><br>    <input type="button" value="第一个"  />    <input type="button" value="上一个"  />    <input type="button" value="下一个"  />    <input type="button" value="最后一个"  />  <script type="text/javascript">  var curTarget = document.getElementById("select1");  function change(target){  alert(target.text);  }  </script>  </body></html> 

访问表单域控件:<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>访问表单域控件</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page">  </head>    <body><form action="" id="d" name="form1" method="post"><input name="username" type="text" /><br><input name="password" type="text" /><br><select name="color"><option value="red">红色</option><option value="blue">蓝色</option></select><input type="button" value="第一个"  /><input type="button" value="第二个"  /><input type="button" value="第三个"  /><input type="button" value="第一个"  /><input type="button" value="第二个"  /><input type="button" value="第三个"  /></form>  </body></html> 

</h2>
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

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