首页 > 解决方案 > 如何在html字符串中插入onClick事件

问题描述

我正在尝试将 onClick 方法添加到 html 字符串,然后使用 insertAdjacentHTML 将其添加到 html 元素

function renderHtml(sendMessage, userId) {
  return `<div class="message-data" onClick='${() => 
  sendMessage(userId)}'>message</div>`
}
let htmlString = renderHtml(this.sendMessage, userId);
document.querySelector('#message-container').insertAdjacentHTML('beforeend', htmlString );

我如何在这个 html 中传递我的函数。

标签: javascripthtmlangularjsreactjs

解决方案


推荐阅读