简体   繁体   English

我想根据条件更改文本颜色

[英]i want to change the text color based on the condition

I am trying to show the text color change dynamically based on the condition that i am using in the code.Below is the code that i am using.Please help me out from this problem. 我试图根据我在代码中使用的条件动态显示文本颜色更改.Below是我正在使用的代码。请帮我解决这个问题。

<span class="pendtext" style="font-size:15px;">{{($duepay->status == 'issued') ? 'PENDING' : (($duepay->status == 'paid') ? 'SUCCESSFUL' : $duepay->status)}}</span>


<script>

if($duepay->status == 'issued'){
  $(".pendtext").css('color','green');
}
else{
  $(".pendtext").css('color','yellow');
}

</script>

$duepay->status is presumably coming from your PHP code so you need to output it in the JS in the same way you have in the HTML: $duepay->status可能来自您的PHP代码,因此您需要以与HTML中相同的方式在JS中输出它:

if ('{{$duepay->status}}' === 'issued'){
  $(".pendtext").css('color', 'green');
}
else{
  $(".pendtext").css('color', 'yellow');
}

Note that this can be shortened using a ternary expression: 请注意,这可以使用三元表达式缩短:

$(".pendtext").css('color', '{{$duepay->status}}' === 'issued' ? 'green' : 'yellow');

Or to keep the logic entirely in the PHP: 或者将逻辑完全保留在PHP中:

$(".pendtext").css('color', '{{$duepay->status == 'issued' ? 'green' : 'yellow'}});

Maybe this : 也许这个:

    <div id="pend" class="pendtext" style="font-size:15px;">

    </div>

    <script>

    if ('{{$duepay->status}}' === 'issued'){
    document.getElementById('pend').innerHTML = "<p>PENDING</p>";
    document.getElementById('pend').style='color:green';
    }
    else{
    document.getElementById('pend').innerHTML = "<p>SUCCESSFUL</p>";
    document.getElementById('pend').style='color:yellow';
    }

    </script>

Why use a JS if you work with PHP? 如果你使用PHP,为什么要使用JS? you can do with PHP too like 你可以用PHP做

<span class="pendtext" style="font-size:15px;color:{{($duepay->status == 'issued')?'green':'yellow'}}">{{($duepay->status == 'issued') ? 'PENDING' : (($duepay->status == 'paid') ? 'SUCCESSFUL' : $duepay->status)}}</span>

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

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