在foreach循环外调用javascript函数

时间:2019-06-05 03:38:18

标签: javascript php ajax laravel

我的JavaScript函数存在问题,当我将脚本放入foreach循环之类的代码中时,我在下面的一切工作正常,但是我认为它应该在外部,因此任何人都可以给我提供解决方案,我感谢它

@foreach($messages->reverse() as $message )
    <ul class="id{{$message->id}}" data-id="{{$message->id}}">
        <li class="message">
            <div class="text {{ ($message->to!=Auth::user()->id)?'not_owner':'owner'}}">
                {{$message->text}}
            </div><br>
        </li>
        <li class="message">
            <div class="time {{ ($message->to!=Auth::user()->id)?'not_owner':'owner'}}">
                <div style="display: flex; flex-wrap: nowrap;">
                @if($message->to!=Auth::user()->id)
                    <div style="display: table">
                        <a onclick="togglediv('item{{ $message->id }}')" class="toggle{{$message->id}} hidden" style=" text-decoration: none; margin-right: 5px; cursor: pointer" > <span class="dot"></span>
                            <span class="dot"></span>
                            <span class="dot"></span></a>
                        <div id="item{{ $message->id }}" style="display:none;"><button value="{{$message->id}}" class="btn-remove" >remove</button></div>
                    </div>
                @endif
                    {{ \Carbon\Carbon::parse($message->created_at)->format('M d, h:i')}}
                </div>
            </div>
        </li>
    </ul>
    <script>
        $('.id{{ $message->id }}').hover(function(){
            $('.toggle{{ $message->id }}').toggleClass('hidden');
        });
    </script>
@endforeach

1 个答案:

答案 0 :(得分:1)

选择类别为<ul>的{​​{1}},并获取当前悬停元素的data属性,并隐藏相应的切换类。

id