如何专门为 (.preventDefault()) 和停止传播向此代码添加事件

how do i add an event to this code specifically for (.preventDefault()) and stoppropagation

本文关键字:事件 代码 传播 添加 何专门 preventDefault      更新时间:2023-09-26
function divFunction() {
         $('#edit-modal').modal();
}

如何在此代码中添加事件?我已经尝试过这个,但它在火狐中显示错误:

类型错误:事件未定义

function divFunction(event) {
   event.stopPropagation(); 
   var postBody = event.target.parentNode.parentNode.childNodes[1].textContent;
   $('#post-body').val(postBody);   
   $('#edit-modal').modal();
}

这就是我调用函数的方式:

@if(Auth::user()== $post->user) 
    <a href="#" name="edit" class="edit" onClick="divFunction()">Edit | </a> 
    <a href="{{ route('post.delete', ['post_id' => $post->id]) }} ">Delete | </a> 
@endif

你需要将事件对象传递到函数中

<a href="#" name="edit" class="edit" onClick="divFunction(event)">Edit | </a>