我爱编程

2017-04-07 jQuery 学习笔记

2017-04-10  本文已影响0人  GodlinE

jQuery 函数整理

//显示,添加参数表示动画时间,动画同时控制宽高
$("div").show(1000).html('哈哈哈')
//隐藏,添加参数表示动画时间,动画同时控制宽高
$('div').hide(1000)
//html 添加文本,括号内是字符串,无法输入标签
$("div").show(1000).html('哈哈哈')
$(document).ready(function(){})
//功能类似 
window.onload = function(){}
//前者可以多次执行,function(){}是作为参数传递进去,所以并不会覆盖;后者只能执行一次,function(){} 作为值,是赋值操作
//前者加载不包括外部资源图片,只是 DOM 树加载完成之后就执行,比后者执行快
$.noConflict();
//解除之后要使用 jQuery 对象就使用jQuery前缀
jquery.('div').hide(1000)
 $('div').addClass('current');
 $('div').removeClass('current');
$('div').taggleClass('current');
//键值对的形式
 $div.css({
                            'width': '500px',
                            'height': '500px',
                            'background': 'red'
                        })
$div.css('width','500px');
$('input').val('1233');
$('input').val();
var w = $("#small").width()
                var h = $("#small").height()
                var iw = $("#small").innerWidth()
                var ih = $("#small").innerHeight()
                var ow = $("#small").outerWidth()
                var oh = $("#small").outerHeight()
                var owm = $("#small").outerWidth(true)
                var ohm = $("#small").outerHeight(true)
//带参数为设置
$(selector).attr(attribute,value)
$('a').attr('target','_self')
//获取规定参数的属性的值
$(selector).attr(attribute)
//使用函数来设置属性和值
$(selector).attr(attribute,function(index,oldvalue))
//键值对的方式设置多个属性和值
$(selector).attr({attribute:value, attribute:value ...})
$('a').attr({
         'href': 'http://www.baidu.com',
         'target': '_blank'
 })
$("#filter").parent().css("background-color", "blue")
$("#filter").parents().css("background-color", "blue")
$('div').child()
$('div').children()
$(this).css('background', 'red').siblings().css('background', 'green');
//.next(selector)
$("#ul1").next("div").css("background-color", "red")
$(":eq(index)")
$(".class").eq(0).css("background-color", "green")
 $('li').hover(function () {
                        $(this).children('.filmContent').addClass('current').parent().siblings().children('.filmContent').removeClass('current');
                    })
 $('li').hover(function () {
                        console.log($(this).index());
                    })
$(selector).toggle(speed,callback,switch);
 $('.ad').toggle(1000);
$(selector).slideToggle(speed,callback);
$(this).children('.secondMenue').stop().slideToggle(200);
$(this).siblings().children('.secondMenue').stop().slideUp(200);
$(this).siblings().children('.secondMenue').stop().slideDown(200);
$(selector).stop(stopAll,goToEnd)
$('.box2').parent().addBack().css('background', 'cyan');
$('.ad').delay(1000).fadeIn(1000).delay(5000).fadeOut(1000);
$('.ad').delay(1000).fadeIn(1000).delay(5000).fadeOut(1000);
  $('.ad').fadeToggle(1000);
 $('div').animate({
                   'width': '1000px'
               }, 1000, function () {
                   console.log('动画结束');
               });
$(selector).each(function(index,element));
  $('li').each(function () {

//               this  == DOM对象
               $(this).slideUp(2000);

//               console.log(index + value);
           })
var $newTag = $('<li>新增的节点</li>');
$('.red').append($newTag);
$newTag.appendTo($('.red'))
$newTag.prependTo($('.red'));
$('.red').prepend($newTag);
$('.red').before($newTag);
$newTag.innerBefore($('.red'));
$('.red').after($newTag);
$newTag.innerAfter($('.red'));
$('.red>li:first').remove();
$('.red>li:first').empty();
$('.red>li:first').html('');

注意要清空自带属性要使用 val('')

$.trim(content).length <= 0
$('.content').focus();
$('.content').blur();
var $cloneTag = $pTag.clone(true);
var $pTag = $('p');
var $newTag = $('<h1>0000</h1>');
$pTag.replaceWith($newTag)
function test(event){
        console.log(event.target);
}
function test(event){
        console.log(event.type)
}
        function 事件data() {
            $('ul').click({'name': 'sz'}, exec);
            function exec(event) {
                console.log(event.data);
            }
        }
var num = 0;
function test(){
        $('p').click(function(event){
                return num++;
        })
        $('p').click(function(event){
                alert(event.result);
        })
}
//绑定单个事件
$('p').on('click',function(){
        console.log('xxxx');
})

//绑定多个事件,同一个回调函数,中间用空格隔开
$('p').on('click mouseenter',function(event){
        console.log('xxx' + event.type)
})

//绑定多个事件,多个回调函数,用键值对的方式
$('p').on({
        'click':cE,
        'mouseenter':function(){
                console.log('鼠标移入');
        }
})
function cE(){
        console.log('单击事件')
}

//body被点击,小king执行 function
$('body').on('click','.king', function () {
             console.log('我是小king, 处理事件');
         })
$('p').off('click', cE);
$('p').one({
        'click':function(){
                console.log('xxxx');
        }
})

//内部实现原理就是结尾解除绑定
$('p').click(function(){
        console.log('xxxx');
})
$(':submit').click(function (event) {
               console.log('xxxx');
               event.preventDefault();
           })

$(':submit').trigger('click');
$('.box3').triggerHandler('click');
       $('.box').on('click.xx', function () {
            console.log('click.xx');
      })
       $('.box').on('mouseenter.xx', function () {
          console.log('mouseenter.xx');
      })

      $('.box').on('click.lisi', function () {
          console.log('click.lisi');
      })


         function haha() {
             console.log('1');
         }

         $('.box').click(haha)
         $('.box').click(function () {
             console.log('2');
         })



         $('.box').on('click.one', function () {
             console.log('1');
         })
         $('.box').on('click.two', function () {
             console.log('2');
         })


         $('.box').off('.one');



 $('.box').trigger('click.lisi');
上一篇 下一篇

猜你喜欢

热点阅读