【问题标题】:jQuery - if/else statement is not workingjQuery - if/else 语句不起作用
【发布时间】:2021-11-23 04:02:23
【问题描述】:

我正在尝试让 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");
    }
 });

非常感谢任何帮助我进行此修复的帮助。

谢谢

【问题讨论】:

    标签: javascript html jquery css


    【解决方案1】:

    $(this).find('card-decorator-stroke') 返回一个始终为真的 jQuery 对象。

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

    【讨论】:

    • 如果 .card-decorator-brand-curve 添加了 card-decorator-stroke-none 我如何让它工作?它不适合我。当添加 card-decorator-stroke-none 时,我需要 removeClass('brand-curve-stroke') 发生。
    • 回答了我自己的问题!没关系。谢谢!
    【解决方案2】:

    我的大脑可能整天都在写代码,但我终于想通了。

     $('.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');
               }
    });

    【讨论】:

      猜你喜欢
      • 2014-09-15
      • 1970-01-01
      • 2014-03-21
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 2015-05-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多