javascript - Reactjs 和脚本
问题描述
我正在用 reactJS 建立一个博客。然而,危险的SetInnerHTML 不会在博客文章中呈现脚本。
这些博客文章包含 twitter 小部件或 github 嵌入等
有没有办法做到这一点?
解决方案
您始终可以在 index.html 文件中包含这些脚本。如果你想有条件地添加这些脚本文件,你可以这样做。
var script = document.createElement("script");
script.type = "text/javascript";
if (window.matchMedia("(max-width: 568px)").matches) {
script.src = "//path/to/mobile-users.js";
} else {
script.src = "//path/to/desktop-users.js";
}
document.body.appendChild(script);
推荐阅读
- python - ML Pipeline OneHotEncoder 不适合
- css - 在 Bootstrap 中重叠两个 div
- javascript - 在 React 中调用 set state 时如何从数组中获取更新的 ID
- javascript - 如何在 jquery-confirm js 中使用图像
- javascript - 使用eventListener单击某个元素后如何获取返回值?
- swift - 无法运行 sonarqube 中的最后一个脚本 (./run-sonar-swift.sh -v)
- java - 应用程序更新 firebase 实时数据库中的数据,但在 recyclerview 中不显示任何内容
- docker - 如何设置 Squid 以绕过 localnet 上客户端的基本身份验证?
- webauthn - 为什么网络钓鱼网站会要求用户通过 webauthn,而不是直接要求用户名/密码?
- c# - System.ArgumentNullException:值不能为空。参数名称:userManager