zoukankan      html  css  js  c++  java
  • [转]jquery加载页面的方法(页面加载完成就执行)

    jquery加载页面的方法(页面加载完成就执行),建议大家看下windows.onload与$(document).ready之间的区别。
     

    1、$(function(){
      $("#a").click(function(){
        //adding your code here
      });
    });
    2、$(document).ready(function(){
      $("#a").click(function(){
        //adding your code here  
      });
    });
    3、window.onload = function(){
      $("#a").click(function(){
        //adding your code here
      });
    }
    html代码为<input type="button" id="a">点击</input>,且页面需要引用jquery的js文件

    一般的加载页面时调用js方法如下:

    window.onload = function() {
    $("table tr:nth-child(even)").addClass("even"); //这个是jquery代码
    };

    这段代码会在整个页面的document全部加载完成以后执行。不幸的这种方式不仅要求页面的DOM tree全部加载完成,而且要求所有的外部图片和资源全部加载完成。更不幸的是,如果外部资源,例如图片需要很长时间来加载,那么这个js效果就会让用户感觉失效了。

    但是用jquery的方法:

    $(document).ready(function() {

    // 任何需要执行的js特效
    $("table tr:nth-child(even)").addClass("even");
    });

    就仅仅只需要加载所有的DOM结构,在浏览器把所有的HTML放入DOM tree之前就执行js效果。包括在加载外部图片和资源之前。

    还有一种简写的方式:

    $(function() {

    // 任何需要执行的js特效
    $("table tr:nth-child(even)").addClass("even");
    });

    [javascript] view plaincopy
      1. <mce:script type="text/javascript"><!--  
      2.  $(function(){  
      3.  var pn = $("#gotopagenum").val();//#gotopagenum是文本框的id属性  
      4.  location.href = "NewList.aspx?pagenum="+pn;//location.href实现客户端页面的跳转  
      5.  });  
      6. // --></mce:script> 
  • 相关阅读:
    Rust语言学习笔记(11)
    Rust语言学习笔记(10)
    Rust语言学习笔记(9)
    Rust语言学习笔记(8)
    趣味编程:静夜思(Rust版)
    Python sorted()
    Python 魔法方法
    Python filter()
    Python的map和reduce
    Python函数的参数
  • 原文地址:https://www.cnblogs.com/ZhuRenWang/p/4801392.html
Copyright © 2011-2022 走看看