jQuery类名选择器对非原始类名无法进行操作

jq类名选择器对使用jq添加类名的元素无效,也就是说只对原始类名有用……比如我用jq给一个元素加一个类名……之后我想对这个元素进行操作,可是就无法使用类名选择器来操作这个元素了……

这是因为你新添加的类在一开始并不存在,你直接用$().click()就会没有响应,你可以用delegate()方法绑定事件,具体你可以查一下这个函数的使用方法。我这里给个例子,你看看有没有启发

< html  xmlns = "http://www.w3.org/1999/xhtml" >
< head >
     < title >TEST</ title >
     < meta  http-equiv = "Content-Type"  content = "text/html; charset=utf-8"  />
     < script  src = "http://code.jquery.com/jquery-latest.js" ></ script >
     < script >
         $(function () {
             var $comment = $("#comment");
             $(".bigger").click(function () {
                $(".small").addClass('smaller');
                $comment.height($comment.height() + 50);
             });
             $(".msg").delegate('.smaller', 'click', function () {
                $comment.height($comment.height() - 50);
             });
             // 如果用下面的,就没有效果
             // $(".smaller").click(function () {
             //     $comment.height($comment.height() - 50);
             // });
         });
     </ script >
</ head >
< body >
     < form  action= "#">
         < div  class = "msg" >
             < div  class = "caption" >
                 < span  class = "bigger" >bigger</ span >
                 < span  class = "small" >smaller</ span >
             </ div >
             < div  class ="content">
                 < textarea  id = "comment"  rows = "8"  cols = "20" >this is a Jquery test!</ textarea >
             </ div >
         </ div >
     </ form >
</ body >
</ html >

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值