JQuery5.04获取

获取body:  $('body'); 或者 $(document.body);

获取元素标签:$('div');   $('a');

获取ID: $('id');

获取某个元素的某个属性: $('a[data-for]');

获取某个标签的属性及属性值: $('div[data-for="result"]');     $('img[hidefocus="true"]');

获取某个元素下的标签:$('#nv a'); //id为nv下的所有a标签 

            $('#form a').length; //a的长度

获取元素的属性、设置元素的属性值:$('#lg img').attr('src','img/01.jpg') //一个值是获取,两个值是设置

设置多个属性可以用对象:$('#tfCon').attr({clientX:100, clientWidth:'100px', 'date-index':100, a:'b'});

移除属性:$('#tfCon').removeAttr('nackground');

设置class:$('#tfCon').addClass('div1');

移除class: $('#tfCon').addClass('div1');

获取innerHTML,innerText: $('ftCon').html();  $('ftCon').html('<a>百 度</a>'); //没有参数是获取,一个参数是设置

               $('ftCon').text(); $('ftCon').text('设置文本'); $('ftCon').text(123456);

获取,设置css: $('ftCon').css('width','500px'); 

        $('ftCon').css({'500px', height:'200px', background:'blue'}) 

获取css属性:$('ftCon').width(); //width是原始宽度,不包括padding,margin,border

       $('ftCon').outerWidth(); //outerWidth包括padding,margin,border 

       $('ftCon').innerWidth(); //不包括border  

      innerHeight, outerHeight 一样  

toggleClass(): 有class就移除,没有class就添加. 

事件: $('#ftCon').on('click',play);  //有名事件

    function play(){alert(123);};

     $('#ftCon').on('click',function play(){alert(123);};);  //匿名事件

或者这样直接加事件类型 $('#ftCon').click( function(){alert(123)} );  //不需要on

              $(document).mouseover( function(){alert(123)} );

循环:   $.each( 遍历元素, 回调函数 );  

     $.each( $('a'), function(v){ console.log(v); };  );

    //$.each( $('a'), function(元素){ console.log(元素) } );  索引和元素分开显示

    $.each( $('a'), function(index,value){ console.log(index,value)} );

    //$.each( $('a'), function(索引,元素){ console.log(索引,元素) } );  索引和元素一起显示

    $.each( $('a'), function(index,value){ console.log(index, value.innerHTML) } );

    //获取元素的innerHTML,因为是JS元素,不能用html(),如果要用就要把元素转换成JQ元素,

    $.each( $('a'), function(index,value){ console.log(index, $(value).html()) } );

原文地址:https://www.cnblogs.com/leo388/p/4478082.html