【问题标题】:Laravel add class using if statementLaravel 使用 if 语句添加类
【发布时间】:2019-03-23 22:29:26
【问题描述】:

我想使用 Bootstrap 轮播,图片应该来自我的数据库

<div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
    <div class="carousel-inner">
        @foreach($property->photos as $photo)
            <div class="carousel-item">
                <img class="d-block w-100" src="{{$photo->path}}" alt="First slide">
            </div>
        @endforeach 
    </div>
</div>

当我检查页面时,我可以看到图像在那里,但是我看不到任何图像,因为我需要在 div 中使用类 carousel-item 活动的类。

有没有办法可以在第一个框中添加活动的类?

我尝试使用 jQuery 但它不起作用:

$('.carousel-inner .carousel-item:first-child').addClass('active');

【问题讨论】:

  • 你使用的是什么版本的 Laravel?
  • 我使用的是 5.6 版

标签: jquery laravel


【解决方案1】:

您可以使用The loop variable 来完成此操作:

@foreach($property->photos as $photo)
    <div class="carousel-item {{ $loop->first ? 'active' : '' }}">
        <img class="d-block w-100" src="{{$photo->path}}" alt="First slide">
    </div>
@endforeach 

【讨论】:

  • 这也有帮助。感谢支持
【解决方案2】:

你用过

$(document).ready(function(){

喜欢

$(document).ready(function(){
    $('.carousel-inner .carousel-item:first-child').addClass('active');
}

还是没有? 因为要运行 jq 代码,您需要等待文档准备好!

【讨论】:

  • 我使用的是,但是现在您进行了仔细检查,我刚刚注意到我将代码放入了错误的函数中。感谢支持
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多