简体   繁体   English

将函数解析为危险的setinnerhtml

[英]Parsing functions into dangerouslysetinnerhtml

I've currently go an issue with dangerouslysetinnerhtml.我目前遇到了dangerouslysetinnerhtml 的问题。 I'm trying to allow people to edit sections via a js document in react, and these edits appear in a form of a string.我试图允许人们通过 react 中的 js 文档编辑部分,这些编辑以字符串的形式出现。 eg a person could parse the following as a string within the js file:例如,一个人可以将以下内容解析为 js 文件中的字符串:

let html = `<div onClick="(input)=>{console.log(input)}">Click me</div>`

And in my code I'd like to display it as:在我的代码中,我想将其显示为:

<div dangerouslySetInnerHTML={{ __html: html }}></div>;

The div appears just fine, but the 'onClick' function doesn't fire. div 看起来很好,但 'onClick' 函数没有触发。 I've seen recommendations to use HTML Parser, but that's not an option as I'm restricted from using external libraries in this case.我已经看到了使用 HTML Parser 的建议,但这不是一个选项,因为在这种情况下我被限制使用外部库。 Does anyone have any suggestions on how this should work?有没有人对这应该如何工作有任何建议?

Thanks!谢谢!

You can do this way if you really need to.如果你真的需要,你可以这样做。

let html = '<div onclick="console.log(this)">Click me</div>';

And same as you have done in your code.与您在代码中所做的相同。

<div dangerouslySetInnerHTML={{ __html: html }}></div>;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM