• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

javascript – 为每个收藏按键添加事件时,为什么“已收藏“总不能变回”收藏“?

php 搞代码 4年前 (2022-01-25) 18次浏览 已收录 0个评论
文章目录[隐藏]
<code>//为每个收藏添加一个事件var colength=$(".col").length;for(var i=0;i<colength;i++){    $(".col").eq(i).click(function(){      ale<a style="color:transparent">来@源gao*daima.com搞@代#码网</a><strong>搞gaodaima代码</strong>rt($(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("已收藏");       }       })}</code>

回复内容:

<code>//为每个收藏添加一个事件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("已收藏");       }       })}</code>

代码贯通了哦

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

==修改下===

<code>if($(this).text().length==3){   $(this).css("background","white");   $(this).text("收藏");}else if($(this).text().length==2){       $(this).css("background","yellow");   $(this).text("已收藏");}</code>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:javascript – 为每个收藏按键添加事件时,为什么“已收藏“总不能变回”收藏“?
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址