博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jquery知识简单运用
阅读量:4635 次
发布时间:2019-06-09

本文共 3216 字,大约阅读时间需要 10 分钟。

this的用法:

$(function(){    $('input').click(function(){        //$(this).style.background = 'red';   //$(this)是jquery对象,是当前调用click方法的对象$("input");        this.style.background = 'red';    //this是js对象,$中大部分是指js对象    });    });

js与jquery互转:

$(function(){        //jquery->js        $('div')[1].style.background = 'red';        $('div').get(1).style.background = 'red';        //js->jquery        var oDiv = document.getElementById('div2');        $(oDiv).css('background','red');    });

has与contains

$(function(){    $('div:has(p)').css('background','red');//div内包含的p标签    $('div:contains(p)').css('background','red');//div内包含的p内容});

val,html,attr:

$(function(){                $('div').html('123');//非表单元素                $('input').val('1111');//表单元素                $('#box').attr({'lot':'let','money':'less'});//属性            });

 

选择器:

$(function(){        //$('#div1').css('background','aqua');            //$('.red').css('background','pink');            //$('ul li').css('background','white');            //$('ul li').css('background','banana');        //$('ul li:first').css('background','orange');            //$('ul li:last').css('background','orange');            //$('ul li:even').css('background','green');            //$('ul li:eq(2)').css('background','green');            $('ul li:lt(2)').css('background','green');      $('li[type=true]').css('background','yellow');        });

removeClass与addClass:

$(function(){        $('p').removeClass('red').addClass('green');    });

hover里面两个函数(mouseover与moverout):

$(function(){    $('#div1').hover(function(){        $('#div1').css({width:'400px',height:'400px',background:'red'});    },function(){        $('#div1').css({width:'200px',height:'200px',background:'green'});    });});

trim:

$(function(){        var str = ' fd    asfa    dsa     ';        alert('|'+$.trim(str)+'|');   //去掉字符串起始和结尾的空格    });

 取消冒泡:

$(function(){        $(document).click(function(){            alert('document');        });        $('body').click(function(){            alert('body');        });        $('input').click(function(ev){            alert('input');            return false;  //除了取消冒泡外还阻止默认行为            ev.stopPropagation();   //只取消冒泡        });    });

 删除元素:

$(function(){    $('#btn').click(function(){        $('#li1').remove();        });});

事件绑定:

$(function(){           $('input').bind('click',function (){alert(1)});  //bind(事件名,函数名)           $('input').bind('click mouseover',function (){alert(2)});//可以批量加事件           $('input').unbind('click',fn1);//匿名函数可以绑定但不可以解绑定;      });

事件委托:

$(function(){           $('#btn').click(function(){                $('
  • '+$('#txt').val()+'
  • ').prependTo('#ul1');//prependTo添加到后面元素子集的第一个 $('#txt').val(''); }); function fn1(){ //off时不能是匿名函数 alert($(this).html()); } $('#ul1').on('click','li',fn1); $('#ul1').off('click','li',fn1); //off(事件名,子集,函数名) });

    循环:

    $(function(){        $('input').each(function(index,element){            //alert(this == element);//true            alert(index);  //0,1,2相当于for循环的i        });        });

    jquery插件:

            

    find方法:

     

    转载于:https://www.cnblogs.com/yang0902/p/5712658.html

    你可能感兴趣的文章
    织梦教程
    查看>>
    杭电多校 Harvest of Apples 莫队
    查看>>
    【文章】孝心无价 作者:毕淑敏
    查看>>
    mysql中的数据类型
    查看>>
    19-6/28作业:100以内偶数求和
    查看>>
    TKmybatis和mybatisplus哪个好用
    查看>>
    iframe 子父窗口互掉 js
    查看>>
    函数对象 函数嵌套 名称空间与作用域
    查看>>
    SSIS 包部署错误 0xC0010014
    查看>>
    java 第11次作业:你能看懂就说明你理解了——this关键字
    查看>>
    git 代理设置
    查看>>
    ORACLE查询表最近更改的数据
    查看>>
    如果我们不曾相遇
    查看>>
    输入圆的半径,计算并输出圆的周长和面积
    查看>>
    metric learning -- 马氏距离与欧氏距离
    查看>>
    HashMap HashTable ConcurrentHashMap
    查看>>
    2进制 , 8进制 , 10进制 , 16进制 , 介绍 及 相互转换 及 快速转换
    查看>>
    C/C++心得-结构体
    查看>>
    快速解决 GRADLE 项目下载 gradle-*-all.zip 慢的问题
    查看>>
    【第二章】 IoC 之 2.1 IoC基础 ——跟我学Spring3
    查看>>