reactjs 为什么在react中使用dangerouslySetInnerHTML时HTML没有正确呈现

y1aodyip  于 2023-03-08  发布在  React
关注(0)|答案(1)|浏览(310)

我有一个HTML文本定义如下:

const helpContent ="<form name=\"punchout_form\" method=\"post\" action=\"https://openapi.alipaydev.com/gateway.do?charset=UTF-8&method=alipay.trade.page.pay&sign=nKG8mCXQlGKOHezlqu7nM9ZJWskZsYVzWyW4Rm0dS%2BT8A4iWs3A96gwaqcdCwINIYacXQbtErYAM7GNAaqWFTNdl33Xj%2BVbEPqew%2BXO2yoihRU8onV0Omt5eo01PRITGEbclLI5v6VmpBwzbrzMawu63dSXTRWJ2jRga5ZUr1cnvexYQj3NLWH5eQS7ZHXwLhbOFlVOyostdF%2FSd9YJTFNotz25K1eipLI4JdG3%2BtaLLBQsNDahET09KWkxKjALSrZGIT48KLOmKWDMXkXTJzHSxjvOFxYKtBmwRenkRxt1wTWX21LgLH3Bi8k4N1KFNuj4FxrzI%2FJGOa58%2BULO54Q%3D%3D&return_url=http%3A%2F%2Fdomain.com%2FCallBack%2Freturn_url.jsp&notify_url=http%3A%2F%2Fdomain.com%2FCallBack%2Fnotify_url.jsp&version=1.0&app_id=2021000122621126&sign_type=RSA2&timestamp=2023-03-07+02%3A00%3A53&alipay_sdk=alipay-sdk-java-4.35.65.ALL&format=json\">\n<input type=\"hidden\" name=\"biz_content\" value=\"{&quot;extendParams&quot;:&quot;dd&quot;,&quot;outTradeNo&quot;:&quot;dd&quot;,&quot;passbackParams&quot;:&quot;ddd&quot;,&quot;productCode&quot;:&quot;productCode&quot;,&quot;subject&quot;:&quot;ddd&quot;,&quot;sysServiceProviderId&quot;:&quot;ddd&quot;,&quot;totalAmount&quot;:&quot;1&quot;}\">\n<input type=\"submit\" value=\"立即支付\" style=\"display:none\" >\n</form>\n<script>document.forms[0].submit();</script>";

然后我在react "react": "^17.0.1"中呈现这个html文本,如下所示:

<div style={{paddingLeft: 15, textIndent: 2}} dangerouslySetInnerHTML={{__html:helpContent1}}></div>

让我困惑的是UI什么也不显示,我尝试定义一个简单的html文本,如下所示:

const helpContent1 ="<span>fwegewgwgwe</span>";

这工作正常,为什么长文本不能被呈现?我应该做什么使它工作如预期?我错过了什么?

hjqgdpho

hjqgdpho1#

const helpContent ="<form name=\"punchout_form\" method=\"post\" action=\"https://openapi.alipaydev.com/gateway.do?charset=UTF-8&method=alipay.trade.page.pay&sign=nKG8mCXQlGKOHezlqu7nM9ZJWskZsYVzWyW4Rm0dS%2BT8A4iWs3A96gwaqcdCwINIYacXQbtErYAM7GNAaqWFTNdl33Xj%2BVbEPqew%2BXO2yoihRU8onV0Omt5eo01PRITGEbclLI5v6VmpBwzbrzMawu63dSXTRWJ2jRga5ZUr1cnvexYQj3NLWH5eQS7ZHXwLhbOFlVOyostdF%2FSd9YJTFNotz25K1eipLI4JdG3%2BtaLLBQsNDahET09KWkxKjALSrZGIT48KLOmKWDMXkXTJzHSxjvOFxYKtBmwRenkRxt1wTWX21LgLH3Bi8k4N1KFNuj4FxrzI%2FJGOa58%2BULO54Q%3D%3D&return_url=http%3A%2F%2Fdomain.com%2FCallBack%2Freturn_url.jsp&notify_url=http%3A%2F%2Fdomain.com%2FCallBack%2Fnotify_url.jsp&version=1.0&app_id=2021000122621126&sign_type=RSA2&timestamp=2023-03-07+02%3A00%3A53&alipay_sdk=alipay-sdk-java-4.35.65.ALL&format=json\">\n<input type=\"hidden\" name=\"biz_content\" value=\"{&quot;extendParams&quot;:&quot;dd&quot;,&quot;outTradeNo&quot;:&quot;dd&quot;,&quot;passbackParams&quot;:&quot;ddd&quot;,&quot;productCode&quot;:&quot;productCode&quot;,&quot;subject&quot;:&quot;ddd&quot;,&quot;sysServiceProviderId&quot;:&quot;ddd&quot;,&quot;totalAmount&quot;:&quot;1&quot;}\">\n<input type=\"submit\" value=\"立即支付\" style=\"display:none\" >\n</form>\n<script>document.forms[0].submit();</script>";

您的输入具有内联样式style="display:none"因此您的内容正在呈现,但被隐藏。删除该行,您将看到它。

相关问题