jquery的加载函数ready的用法

来源:互联网 发布:淘宝助理主图不显示 编辑:程序博客网 时间:2024/05/14 14:04

<%@ 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 'jquery1.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="js/jquery-1.2.6.js"></script>
  <script type="text/javascript">
  //这是将dom对象转换成jquery对象,ready相当于onload的作用。但是它们两个却有不同
   $(document).ready(function()
   {
    //alert("我爱你");
   });
   $(document).ready(function()
   {
    //alert("真的吗啊?");
   });
   function test1()
   {
    alert("aaaa");
   }
   function test2()
   {
    alert("bbb");
   }
   //window.onload = test1;
   //window.onload = test2;//两个onload事件只能运行一个,但是ready中的两个回调函数都能运行。
   
   
  </script>
  </head>
 
  <body>
    <a href="#">a</a><br>
    <a href="#">a</a><br>
    <a href="#">a</a><br>
    <a href="#">a</a><br>
    <a href="#">a</a><br>
    <p id="pp">真的或假的</p>
    <p>真的或假的</p>
    <script type="text/javascript">
   //这是用javascript
    /*window.onload = function(){
     var arr = document.getElementsByTagName("a");
   for(var i = 0; i < arr.length; i++)
   {
    arr[i].onclick = test;
   }
   
   function test()
   {
    alert("asdsa");
    return false;
   }
 }*/
 //这是jquery的用法
 
  /*$(document).ready(function()
  {
   $("a").click(function()//$("a")能得到所有的链接标签的对象,然后对每一个标签进行相同的alert("aaaa");操作
   {
    alert("aaaa");
    
   }); 
  });*/
  //将dom对象转换成jquery对象
  $(document).ready(function()
  {
  
   var pElement = document.getElementsByTagName("p")[0];
   //alert(pElement.innerHTML);
   //将dom对象转换成jquery对象
   //alert($(pElement).html());
   //alert($("p").html());
  });
  //这是将jquery对象转换成dom对象
  window.onload = function()
  {
  
   var pt = $("#pp");//这个是一个是通过jquery得到id的对象,得到的是数组。而$("p")注意:这种方式得到也是一个数组,但是不要#。
   alert(pt[0].innerHTML);//这是将jquery对象转换成dom对象
   var t = pt.get(0);//这是将jquery对象转换成dom对象
   alert("测试");
   alert(t.innerHTML);
  }
 
    </script>
  </body>
</html>

原创粉丝点击