javascript - 为每个收藏按键添加事件时,为什么“已收藏“总不能变回”收藏“?
ringa_lee
ringa_lee 2017-04-10 15:45:55
[JavaScript讨论组]
//为每个收藏添加一个事件
var colength=$(".col").length;
for(var i=0;i<colength;i++){
    $(".col").eq(i).click(function(){
      alert($(this).text().length);//这地方提示是正常的,
//但当出现3时,并没有执行第一个if,这是为什么呢?

    if($(this).text().length==3){
       $(this).css("background","white");
       $(this).text("收藏");
        }
    if($(this).text().length==2){
           $(this).css("background","yellow");
       $(this).text("已收藏");
       }
       
})
}
ringa_lee
ringa_lee

ringa_lee

全部回复(1)
怪我咯

代码贯通了哦

if($(this).text().length==3){
   $(this).css("background","white");
   $(this).text("收藏");
}
//长度编程2
if($(this).text().length==2){
       $(this).css("background","yellow");
   $(this).text("已收藏");
}

==修改下===

if($(this).text().length==3){
   $(this).css("background","white");
   $(this).text("收藏");
}else if($(this).text().length==2){
       $(this).css("background","yellow");
   $(this).text("已收藏");
}
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号