jQuery对象和DOM对象的转换实例详解
<%@ 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>My JSP 'example01.jsp' starting page</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript"
src="${pageContext.request.contextPath}/js/jquery-1.7.1.js">
</script>
</head>
<body>
用户名:<input type="text" id="name"/>
</body>
</html>
<script type="text/javascript">
//dom对象和jQquery对象的转换
//窗体加载完毕之后触发的事件
$(document).ready(function() {
//根据id获取dom对象
var domInput = document.getElementById("name");
domInput.value="laowang";
//根据id获取jQuery对象 如果获取的是jQuery对象,最好在前面加上$ 是一个约定
var $jInput = $("#name");
//修改文本框的值 ,在jQuery中用val
$jInput.val("hello");
//dom对象转换成jQuery对象
var $dInput = $(domInput);
$dInput.val("nini");
//把jQuery转换成dom对象 jQuery本身是一个数组对象,可以通过 index[0]来获取dom
var dInput = $jInput[0];
dInput.value="nilaiil";
//第二种把jQuery转换为dom的方法
var dInput = $jInput.get(0);
dInput.value="老";
});
</script>