如何在div html中编写条件语句?

新手上路,请多包涵

我需要根据条件显示 2 个 div,例如如果值为“1”则显示一个 div,否则如果值为“0”则显示另一个 div。下面是我的代码:

 <div class="csv" <?php if($campaign[0]->method != 'CSV'){ echo "style='display:none'"; } ?> >
 -----------
 ------------
</div>

<div class="api" <?php if($campaign[0]->method != 'API'){ echo "style='display:none'"; } ?> >
 ----------
 ----------
</div>

代码看起来不错,但不能正常工作。我试图在 Laravel 5.4 框架中实现这一点。任何帮助,将不胜感激。谢谢

原文由 Prasad Patel 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 280
1 个回答

你可以像这样内联 if 语句:

 <div class="csv" <?php echo ($campaign[0]->method != 'CSV' ? 'style="display: none;" : ''); ?>></div>

() - starts the if statement and closes
?  - is the if condition is met
:  - is the else

原文由 treyBake 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题