几种获取元素的方式

1 从集合中通过指定的序号获取元素

html:

 

ExpandedBlockStart.gif View Code
< div >
    
< p > 0 </ p >
    
< p > 1 </ p >
    
< p > 2 </ p >
    
< p > 3 </ p >
    
< p > 4 </ p >
    
< p > 5 </ p >
    
< p > 6 </ p >
    
< p > 7 </ p >
    
</ div >

 JS

 

ExpandedBlockStart.gif View Code
< script type = " text/javascript " >
      jQuery(function(){
        $(
" p " ).eq( 2 ).css( " color " , " red " );
        $(
" p " ).eq( 3 ).css( " color " , " red " );
      })
   
</ script >

 

2 获取指定条件一致和指定范围的元素

html:

 

ExpandedBlockStart.gif View Code
   < div >
    
< p > 0 </ p >
    
< p > 1 </ p >
    
< class ="center" > 2 </ p >
    
< class ="center" > 3 </ p >
    
< p > 4 </ p >
    
< p > 5 </ p >
    
< p > 6 </ p >
    
< p > 7 </ p >
    
</ div >

 

JS

 

ExpandedBlockStart.gif View Code
   < script type = " text/javascript " >
      jQuery(
function (){
      $(
" p " ).filter( ' .center ' ).css( " color " , " red " ); 
      })
      $(
function (){
        $(
" p " ).slice( 5 , 7 ).css( " color " , " yellow " );
      })
   
< / script>

 

3 获取与条件表达式一致的元素

html:

 

ExpandedBlockStart.gif View Code
< div >
    
< p > 0 </ p >
    
< p > 1 </ p >
    
< class ="center" > 2 </ p >
    
< class ="center" > 3 </ p >
    
< p > 4 </ p >
    
< class ="aa" > 5 </ p >
    
< p > 6 </ p >
    
< p > 7 </ p >
    
</ div >

js

 

ExpandedBlockStart.gif View Code
< script type = " text/javascript " >
      jQuery(
function (){
        $(
" p " ).each( function (){
           
switch ( true ){
               
case  $( this ).is( " .center " ):
                    $(
this ).css( " color " , " red " );
                    
break ;
               
case  $( this ).is( " .aa " ):
                    $(
this ).css( " color " , " yellow " );
                    
break ;
           }
         
        })
      })
   
< / script>

 

4 获取元素的上一个元素和下一个元素

Html:

 

ExpandedBlockStart.gif View Code
< div  id ="aa" >
    
< p > 1号 </ p >
    
< class ="yes" > 2号 </ p >
    
< p > 3号 </ p >
    
< p > 4号 </ p >
    
< p > 5号 </ p >
    
< class ="yes" > 6号 </ p >
    
< class ="yes" > 7号 </ p >
    
</ div >

js

ExpandedBlockStart.gif View Code
     // 获取元素的下一个元素
   jQuery( function (){
   $(
" p " ).next( " .yes " ).css( " color " , " red " );
       })
   
// 获取元素的上一个元素    
       jQuery( function (){
         $(
" p " ).prev( " .yes " ).css( " color " , " red " );
       })

 

5 获取元素的父元素和子元素

html:

 

ExpandedBlockStart.gif View Code
  < div id = " aa " >
    
< p > 1号 </ p >
    
< class = " yes " > 2号 </ p >
    
< p > 3号 </ p >
    
< p > 4号 </ p >
    
< p > 5号 </ p >
    
< class = " yes " > 6号 </ p >
    
< class = " yes " > 7号 </ p >
    
</ div >

 

js

 

ExpandedBlockStart.gif View Code
  // 获取元素的父元素    
       jQuery( function (){
         $(
" p " ).parent().css( " color " , " red " );
       })

  
// 获取元素的子元素    
       jQuery( function (){
         $(
" #aa " ).children( " .yes " ).css( " color " , " red " );
       })

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值