@foreach($postsList as $post)
<div id="post_div_{{$id}}" style="font-weight:bold" class="p-6 bg-white border-b border-gray-200">
@php
$id=$post->id;
@endphp
<strong>{{$post->title}}</strong>
<a>Added: {{$post->created_at}}</a>
<form method="POST" action="/posts/delete/{{$post->id}}">
<button id="DeleteButton" onclick="deletePost($id)" type="button">Delete</button>
</form>
<button onclick="editPost($id)" type="button">Edit</button>
<div id="all_div_{{$id}}" class="p-6 bg-white border-b border-gray-200" style="display: none;">@include('editPost', ['post'=>$post])</div>
<script type="text/javascript">
function deletePost($id) {
document.getElementById('post_div_' + {
$id
}).style.display = "none";
}
function editPost($id) {
document.getElementById('all_div_' + {
$id
}).style.display = "block";
}
</script>
</div>
@endforeach
@foreach($postsList as $post)
@php
$id = $post->id;
@endphp
<div id="post_div_{{$id}}" style="font-weight:bold" class="p-6 bg-white border-b border-gray-200">
<strong>{{ $post->title }}</strong>
<a>Added: {{ $post->created_at }}</a>
<form method="POST" action="/posts/delete/{{ $post->id }}">
<button id="DeleteButton" onclick="deletePost({{ $id }})" type="button">Delete</button>
</form>
<button onclick="editPost({{ $id }})" type="button">Edit</button>
<div id="all_div_{{ $id }}" class="p-6 bg-white border-b border-gray-200" style="display: none;">
@include('editPost', ['post' => $post])
</div>
</div>
@endforeach
<script type="text/javascript">
function deletePost(id) {
document.getElementById('post_div_' + id).style.display = "none";
}
function editPost(id) {
document.getElementById('all_div_' + id).style.display = "block";
}
</script>
每个 post_div_{{ $id }} 和 all_div_{{ $id }} div 都会根据帖子的 $id 获取唯一的 ID。
deletePost({{ $id }}) 和 editPost({{ $id }}) 调用将 $id 传递到 JavaScript 函数中,然后根据动态生成的 ID 访问和操作特定的 div 元素。