繁体   English   中英

jQuery - if/else 语句不起作用

[英]jQuery - if/else statement is not working

我正在尝试让 if/else 语句在 jQuery 中工作。 根据用户在我们平台上的选择。card-decorator-brand-curve 添加了 card-decorator-stroke 或 card-decorator-stroke-none。

card-decorator-stroke 按我的意愿工作,但 card-decorator-stroke-none 没有。

JS 下面:

 $('.card-decorator-brand-curve').each(function() { if ($(this).find('card-decorator-stroke')) { $(this).removeClass('card-decorator-stroke'); $(this).addClass('brand-curve-stroke'); console.log("test me"); } else { $(".card-decorator-stroke-none").removeClass('brand-curve-stroke'); console.log("test me 2"); } });

任何帮助我进行此修复的帮助都将不胜感激。

谢谢

$(this).find('card-decorator-stroke')返回一个 jQuery object ,它总是真实的。

if($(this).find('card-decorator-stroke').length)

我的大脑可能整天都在编码,但我终于弄明白了。

 $('.card-decorator-brand-curve').each(function() { if ($('.card-decorator-brand-curve').hasClass('card-decorator-stroke')){ $('.card-decorator-brand-curve').addClass('brand-curve-stroke'); $('.card-decorator-brand-curve').removeClass('card-decorator-stroke'); console.log('test'); } else { $('.card-decorator-stroke-none').removeClass('brand-curve-stroke'); console.log('test me'); } });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM