zoukankan      html  css  js  c++  java
  • 前端基础之jquery

    http://www.cnblogs.com/yuanchenqi/articles/6936986.html

    前端基础之jquery

     

    一 jQuery是什么? 

    [1]   jQuery由美国人John Resig创建,至今已吸引了来自世界各地的众多 javascript高手加入其team。

    [2]   jQuery是继prototype之后又一个优秀的Javascript框架。其宗旨是——WRITE LESS,DO MORE!

    [3]  它是轻量级的js库(压缩后只有21k) ,这是其它的js库所不及的,它兼容CSS3,还兼容各种浏览器

    [4]  jQuery是一个快速的,简洁的javaScript库,使用户能更方便地处理HTMLdocuments、events、实现动画效果,并且方便地为网站提供AJAX交互。

    [5]  jQuery还有一个比较大的优势是,它的文档说明很全,而且各种应用也说得很详细,同时还有许多成熟的插件可供选择。

    二 什么是jQuery对象?

    jQuery 对象就是通过jQuery包装DOM对象后产生的对象。jQuery 对象是 jQuery 独有的如果一个对象是 jQuery 对象那么它就可以使用 jQuery 里的方法: $(“#test”).html();

    复制代码
    $("#test").html() 
    意思是指:获取ID为test的元素内的html代码。其中html()是jQuery里的方法 这段代码等同于用DOM实现代码: document.getElementById(" test ").innerHTML; 虽然jQuery对象是包装DOM对象后产生的,但是jQuery无法使用DOM对象的任何方法,同理DOM对象也不能使用jQuery里的方法.乱使用会报错 约定:如果获取的是 jQuery 对象, 那么要在变量前面加上$. var $variable = jQuery 对象 var variable = DOM 对象 $variable[0]:jquery对象转为dom对象 $("#msg").html(); $("#msg")[0].innerHTML
    复制代码

     jquery的基础语法:$(selector).action()     

     参考:http://jquery.cuishifeng.cn/

    三 寻找元素(选择器和筛选器) 

    3.1   选择器

    3.1.1 基本选择器      

    1
    $("*")  $("#id")   $(".class")  $("element")  $(".class,p,div")

    3.1.2 层级选择器   

    1
    $(".outer div")  $(".outer>div")   $(".outer+div")  $(".outer~div")

    3.1.3 基本筛选器  

    1
    $("li:first")  $("li:eq(2)")  $("li:even") $("li:gt(1)")

    3.1.4 属性选择器

    1
    $('[id="div1"]')   $('["alex="sb"][id]')

    3.1.5 表单选择器      

    1
    $("[type='text']")----->$(":text")         注意只适用于input标签  : $("input:checked")

    3.1.6 表单属性选择器

        :enabled
        :disabled
        :checked
        :selected
     View Code

    3.2 筛选器

    3.2.1  过滤筛选器    

    1
    $("li").eq(2)  $("li").first()  $("ul li").hasclass("test")

    3.2.2  查找筛选器  

    复制代码
     查找子标签:         $("div").children(".test")      $("div").find(".test")  
                                   
     向下查找兄弟标签:    $(".test").next()               $(".test").nextAll()     
    $(".test").nextUntil() 向上查找兄弟标签: $("div").prev() $("div").prevAll()
    $("div").prevUntil() 查找所有兄弟标签: $("div").siblings()
    查找父标签: $(".test").parent() $(".test").parents()
    $(".test").parentUntil()
    复制代码

    四 操作元素(属性,css,文档处理)

    4.1 事件

    页面载入

    1
    2
    ready(fn)  // 当DOM载入就绪可以查询及操纵时绑定一个要执行的函数。
    $(document).ready(function(){}) -----------> $(function(){})  

    事件绑定

    //语法:  标签对象.事件(函数)    
    eg: $("p").click(function(){})

    事件委派:

    $("").on(eve,[selector],[data],fn)  // 在选择元素上绑定一个或多个事件的事件处理函数。
    复制代码
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
    </ul>
    <hr>
    <button id="add_li">Add_li</button>
    <button id="off">off</button>
    
    <script src="jquery.min.js"></script>
    <script>
        $("ul li").click(function(){
            alert(123)
        });
    
        $("#add_li").click(function(){
            var $ele=$("<li>");
            $ele.text(Math.round(Math.random()*10));
            $("ul").append($ele)
    
        });
    
    
    //    $("ul").on("click","li",function(){
    //        alert(456)
    //    })
    
         $("#off").click(function(){
             $("ul li").off()
         })
        
    </script>
    复制代码

    事件切换

    hover事件:

    一个模仿悬停事件(鼠标移动到一个对象上面及移出这个对象)的方法。这是一个自定义的方法,它为频繁使用的任务提供了一种“保持在其中”的状态。

    over:鼠标移到元素上要触发的函数

    out:鼠标移出元素要触发的函数

     View Code

    4.2 属性操作

    复制代码
    --------------------------CSS类
    $("").addClass(class|fn)
    $("").removeClass([class|fn])
    --------------------------属性 $("").attr(); $("").removeAttr(); $("").prop(); $("").removeProp();
    --------------------------HTML代码/文本/值 $("").html([val|fn]) $("").text([val|fn]) $("").val([val|fn|arr])
    --------------------------- $("#c1").css({"color":"red","fontSize":"35px"})
    复制代码

    attr方法使用:

     View Code

    4.3 each循环

    我们知道,

    1
    $("p").css("color","red")  

    是将css操作加到所有的标签上,内部维持一个循环;但如果对于选中标签进行不同处理,这时就需要对所有标签数组进行循环遍历啦

    jquery支持两种循环方式:

    方式一

    格式:$.each(obj,fn)

    li=[10,20,30,40];
    dic={name:"yuan",sex:"male"};
    $.each(li,function(i,x){
        console.log(i,x)
    });

    方式二

    格式:$("").each(fn)

    $("tr").each(function(){
        console.log($(this).html())
    })

    其中,$(this)代指当前循环标签。

    each扩展

     View Code

    4.4 文档节点处理

    复制代码
    //创建一个标签对象
        $("<p>")
    
    
    //内部插入
    
        $("").append(content|fn)      ----->$("p").append("<b>Hello</b>");
        $("").appendTo(content)       ----->$("p").appendTo("div");
        $("").prepend(content|fn)     ----->$("p").prepend("<b>Hello</b>");
        $("").prependTo(content)      ----->$("p").prependTo("#foo");
    
    //外部插入
    
        $("").after(content|fn)       ----->$("p").after("<b>Hello</b>");
        $("").before(content|fn)      ----->$("p").before("<b>Hello</b>");
        $("").insertAfter(content)    ----->$("p").insertAfter("#foo");
        $("").insertBefore(content)   ----->$("p").insertBefore("#foo");
    
    //替换
        $("").replaceWith(content|fn) ----->$("p").replaceWith("<b>Paragraph. </b>");
    
    //删除
    
        $("").empty()
        $("").remove([expr])
    
    //复制
    
        $("").clone([Even[,deepEven]])
    复制代码

    4.5 动画效果

    显示隐藏

     View Code

    滑动

     View Code

    淡入淡出

     View Code

    回调函数

     View Code

    4.6 css操作

    css位置操作

            $("").offset([coordinates])
            $("").position()
            $("").scrollTop([val])
            $("").scrollLeft([val])

    示例1:

     View Code

    示例2:

     View Code

    示例3:

     View Code

    尺寸操作

            $("").height([val|fn])
            $("").width([val|fn])
            $("").innerHeight()
            $("").innerWidth()
            $("").outerHeight([soptions])
            $("").outerWidth([options])

    示例:

     View Code

    扩展方法 (插件机制)

    jQuery.extend(object)

    扩展jQuery对象本身。

    用来在jQuery命名空间上增加新函数。 

    在jQuery命名空间上增加两个函数:

    复制代码
    <script>
        jQuery.extend({
          min: function(a, b) { return a < b ? a : b; },
          max: function(a, b) { return a > b ? a : b; }
    });
    
    
        jQuery.min(2,3); // => 2
        jQuery.max(4,5); // => 5
    </script>
    复制代码

    jQuery.fn.extend(object)

    扩展 jQuery 元素集来提供新的方法(通常用来制作插件)

    增加两个插件方法:

    复制代码
    <body>
    
    <input type="checkbox">
    <input type="checkbox">
    <input type="checkbox">
    
    <script src="jquery.min.js"></script>
    <script>
        jQuery.fn.extend({
          check: function() {
             $(this).attr("checked",true);
          },
          uncheck: function() {
             $(this).attr("checked",false);
          }
        });
    
    
        $(":checkbox:gt(0)").check()
    </script>
    
    </body>
    复制代码
  • 相关阅读:
    Windows下查看dll被哪个进程调用
    mfc笔记之string,wstring,CString
    查看用户主目录
    我看Windows 8 RT
    C# 性能优化之斤斤计较篇 一
    曾经生活过的城市
    大型.NET项目的目录、编译和版本管理实践 四
    大型.NET项目的目录、编译和版本管理实践 五
    大型.NET项目的目录、编译和版本管理实践 二
    大型.NET项目的目录、编译和版本管理实践 一
  • 原文地址:https://www.cnblogs.com/wangmo/p/8057013.html
Copyright © 2011-2022 走看看